• 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;
            }
        }
    

      

  • 相关阅读:
    SQL查询最近12个月的数据量 没有补0
    SQL查询当前月每天的数据量,没有补0
    Mac上安装mysqlclient报错:OSError: mysql_config not found
    Vue packages version mismatch: 版本始终不对
    python 时间格式2019-08-18T16:00:00.000000Z转换成常见的格式
    前端css的flex布局
    用vim编辑文件时出现错误:E325: ATTENTION的问题与解决方法
    报错:AssertionError: `basename` argument not specified, and could not automatically determine the name from the viewset, as it does not have a `.queryset` attribute.
    PDF打印:html2canvas+jspdf
    python list的Avg、WAvg、Max、Min
  • 原文地址:https://www.cnblogs.com/yaoxiaodan/p/5574278.html
Copyright © 2020-2023  润新知