• SQLite帮助类SQlitehelper 实现对SQLite数据的增删改查


     public class SQLiteHelper
        {
    
            public const string sConn = "Data Source=" + @"path";
    
            /// <summary>
            /// 查询,返回object ,得到查询结果的首行首列的值,若没有首行首列返回NUll
            /// </summary>
            /// <param name="sql">查询语句</param>
            /// <param name="parameters">可选参数</param>
            /// <returns></returns>
            public static object ExecuteScalar(string sql, params SQLiteParameter[] parameters)
            {
                return ExecuteScalar(sql, CommandType.Text, parameters);
    
            }
    
            /// <summary>
            /// 查询,返回object  ,执行SQl语句,得到查询结果的首行首列,若没有首行首列返回null 
            /// </summary>
            /// <param name="sql">查询语句</param>
            /// <param name="type">如何解释命令字符串</param>
            /// <param name="parameters">可选的参数</param>
            /// <returns></returns>
            public static object ExecuteScalar(string sql, CommandType type, params SQLiteParameter[] parameters)
            {
                using (SQLiteConnection conn = new SQLiteConnection(sConn))
                {
                    conn.Open();
                    SQLiteCommand cmd = new SQLiteCommand(sql, conn);
    
                    cmd.CommandType = type;
                    cmd.Parameters.AddRange(parameters);
                    object obj = cmd.ExecuteScalar();
                    cmd.Parameters.Clear();
                    return obj;
                }
            }
    
          /// <summary>
          /// 执行增、删、改
          /// </summary>
          /// <param name="sql">sql语句</param>
          /// <param name="parameters">可选参数</param>
          /// <returns>返回int 得到受影响的行数</returns>
            public static int ExecuteNonQuery(string sql, params SQLiteParameter[] parameters)
            {
                return ExecuteNonQuery(sql, CommandType.Text, parameters);
            }
            /// <summary>
            /// 执行增、删、改
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="type">如何解释命令字符串</param>
            /// <param name="parameters">可选参数</param>
            /// <returns></returns>
            public static int ExecuteNonQuery(string sql, CommandType type, params SQLiteParameter[] parameters)
            {
                using (SQLiteConnection conn = new SQLiteConnection(sConn))
                {
                    conn.Open();
                    SQLiteCommand cmd = new SQLiteCommand(sql, conn);
                    cmd.CommandType = type;
                    cmd.Parameters.AddRange(parameters);
                    int num = cmd.ExecuteNonQuery();
                    cmd.Parameters.Clear();
                    return num;
                }
            }
    
    
            /// <summary>
            /// 断开式查询,查询多个列
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="parameters">可选参数</param>
            /// <returns>返回DataTable类型</returns>
            public static DataTable ExecuteTable(string sql, params SQLiteParameter[] parameters)
            {
                return ExecuteTable(sql, CommandType.Text, parameters);
            }
    
            /// <summary>
            /// 断开式查询,查询结果可为多个列
            /// </summary>
            /// <param name="sql">sql语句</param>
            /// <param name="type">如何解释命令字符串</param>
            /// <param name="parameters">可选参数</param>
            /// <returns></returns>
            public static DataTable ExecuteTable(string sql, CommandType type, params SQLiteParameter[] parameters)
            {
                SQLiteConnection conn = new SQLiteConnection(sConn);
                conn.Open();
                SQLiteCommand cmd = new SQLiteCommand(sql, conn);
                cmd.CommandType = type;
                cmd.Parameters.AddRange(parameters);
                SQLiteDataAdapter sda = new SQLiteDataAdapter(cmd);
                DataSet ds = new DataSet();
                sda.Fill(ds);
                conn.Close();
                DataTable dt = ds.Tables[0];
                return dt;
            }
        }
    

      

  • 相关阅读:
    解决方案 git@github.com出现Permission denied (publickey)
    github设置添加SSH
    base64是啥原理
    PHP面试题:HTTP中POST、GET、PUT、DELETE方式的区别
    PHP中put和post区别
    常用的微信编辑器
    局域网内一台电脑的ip地址自己会变,怎样让它不变
    Trendalyzer is an information visualization software
    FineReport报表和水晶报表的比较
    x
  • 原文地址:https://www.cnblogs.com/yaoxiaodan/p/5574278.html
Copyright © 2020-2023  润新知