[C#] C#封装的Sqlite访问类 →→→→→进入此内容的聊天室

来自 , 2019-04-09, 写在 C#, 查看 122 次.
URL http://www.code666.cn/view/2bdfb48c
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized;
  4. using System.Data;
  5. using System.Configuration;
  6. using System.Data.SQLite;
  7. namespace DAL
  8. {
  9.     internal abstract class DbHelperSQLite
  10.     {      
  11.         public static string connectionString = "Data Source=" + AppDomain.CurrentDomain.BaseDirectory + @"dataleaf.db;Version=3;";
  12.         public DbHelperSQLite()
  13.         {
  14.         }
  15.  
  16.  
  17.         #region 公用方法
  18.        
  19.         public static int GetMaxID(string FieldName, string TableName)
  20.         {
  21.             string strsql = "select max(" + FieldName + ")+1 from " + TableName;
  22.             object obj = GetSingle(strsql);
  23.             if (obj == null)
  24.             {
  25.                 return 1;
  26.             }
  27.             else
  28.             {
  29.                 return int.Parse(obj.ToString());
  30.             }
  31.         }
  32.         public static bool Exists(string strSql)
  33.         {
  34.             object obj = GetSingle(strSql);
  35.             int cmdresult;
  36.             if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  37.             {
  38.                 cmdresult = 0;
  39.             }
  40.             else
  41.             {
  42.                 cmdresult = int.Parse(obj.ToString());
  43.             }
  44.             if (cmdresult == 0)
  45.             {
  46.                 return false;
  47.             }
  48.             else
  49.             {
  50.                 return true;
  51.             }
  52.         }
  53.         public static bool Exists(string strSql, params SQLiteParameter[] cmdParms)
  54.         {
  55.             object obj = GetSingle(strSql, cmdParms);
  56.             int cmdresult;
  57.             if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  58.             {
  59.                 cmdresult = 0;
  60.             }
  61.             else
  62.             {
  63.                 cmdresult = int.Parse(obj.ToString());
  64.             }
  65.             if (cmdresult == 0)
  66.             {
  67.                 return false;
  68.             }
  69.             else
  70.             {
  71.                 return true;
  72.             }
  73.         }
  74.          
  75.         #endregion
  76.  
  77.         #region  执行简单SQL语句
  78.  
  79.         /// <summary>
  80.         /// 执行SQL语句,返回影响的记录数
  81.         /// </summary>
  82.         /// <param name="SQLString">SQL语句</param>
  83.         /// <returns>影响的记录数</returns>
  84.         public static int ExecuteSql(string SQLString)
  85.         {
  86.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  87.             {
  88.                 using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  89.                 {
  90.                     try
  91.                     {
  92.                         connection.Open();
  93.                         int rows = cmd.ExecuteNonQuery();
  94.                         return rows;
  95.                     }
  96.                     catch (System.Data.SQLite.SQLiteException E)
  97.                     {
  98.                         connection.Close();
  99.                         throw new Exception(E.Message);
  100.                     }
  101.                 }
  102.             }
  103.         }
  104.  
  105.         /// <summary>
  106.         /// 执行多条SQL语句,实现数据库事务。
  107.         /// </summary>
  108.         /// <param name="SQLStringList">多条SQL语句</param>    
  109.         public static void ExecuteSqlTran(ArrayList SQLStringList)
  110.         {
  111.             using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  112.             {
  113.                 conn.Open();
  114.                 SQLiteCommand cmd = new SQLiteCommand();
  115.                 cmd.Connection = conn;
  116.                 SQLiteTransaction tx = conn.BeginTransaction();
  117.                 cmd.Transaction = tx;
  118.                 try
  119.                 {
  120.                     for (int n = 0; n < SQLStringList.Count; n++)
  121.                     {
  122.                         string strsql = SQLStringList[n].ToString();
  123.                         if (strsql.Trim().Length > 1)
  124.                         {
  125.                             cmd.CommandText = strsql;
  126.                             cmd.ExecuteNonQuery();
  127.                         }
  128.                     }
  129.                     tx.Commit();
  130.                 }
  131.                 catch (System.Data.SQLite.SQLiteException E)
  132.                 {
  133.                     tx.Rollback();
  134.                     throw new Exception(E.Message);
  135.                 }
  136.             }
  137.         }
  138.         /// <summary>
  139.         /// 执行带一个存储过程参数的的SQL语句。
  140.         /// </summary>
  141.         /// <param name="SQLString">SQL语句</param>
  142.         /// <param name="content">参数内容,比如一个字段是格式复杂的文章,有特殊符号,可以通过这个方式添加</param>
  143.         /// <returns>影响的记录数</returns>
  144.         public static int ExecuteSql(string SQLString, string content)
  145.         {
  146.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  147.             {
  148.                 SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
  149.                 SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
  150.                 myParameter.Value = content;
  151.                 cmd.Parameters.Add(myParameter);
  152.                 try
  153.                 {
  154.                     connection.Open();
  155.                     int rows = cmd.ExecuteNonQuery();
  156.                     return rows;
  157.                 }
  158.                 catch (System.Data.SQLite.SQLiteException E)
  159.                 {
  160.                     throw new Exception(E.Message);
  161.                 }
  162.                 finally
  163.                 {
  164.                     cmd.Dispose();
  165.                     connection.Close();
  166.                 }
  167.             }
  168.         }
  169.         /// <summary>
  170.         /// 向数据库里插入图像格式的字段(和上面情况类似的另一种实例)
  171.         /// </summary>
  172.         /// <param name="strSQL">SQL语句</param>
  173.         /// <param name="fs">图像字节,数据库的字段类型为image的情况</param>
  174.         /// <returns>影响的记录数</returns>
  175.         public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
  176.         {
  177.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  178.             {
  179.                 SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
  180.                 SQLiteParameter myParameter = new SQLiteParameter("@fs", DbType.Binary);
  181.                 myParameter.Value = fs;
  182.                 cmd.Parameters.Add(myParameter);
  183.                 try
  184.                 {
  185.                     connection.Open();
  186.                     int rows = cmd.ExecuteNonQuery();
  187.                     return rows;
  188.                 }
  189.                 catch (System.Data.SQLite.SQLiteException E)
  190.                 {
  191.                     throw new Exception(E.Message);
  192.                 }
  193.                 finally
  194.                 {
  195.                     cmd.Dispose();
  196.                     connection.Close();
  197.                 }
  198.             }
  199.         }
  200.  
  201.         /// <summary>
  202.         /// 执行一条计算查询结果语句,返回查询结果(object)。
  203.         /// </summary>
  204.         /// <param name="SQLString">计算查询结果语句</param>
  205.         /// <returns>查询结果(object)</returns>
  206.         public static object GetSingle(string SQLString)
  207.         {
  208.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  209.             {
  210.                 using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
  211.                 {
  212.                     try
  213.                     {
  214.                         connection.Open();
  215.                         object obj = cmd.ExecuteScalar();
  216.                         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  217.                         {
  218.                             return null;
  219.                         }
  220.                         else
  221.                         {
  222.                             return obj;
  223.                         }
  224.                     }
  225.                     catch (System.Data.SQLite.SQLiteException e)
  226.                     {
  227.                         connection.Close();
  228.                         throw new Exception(e.Message);
  229.                     }
  230.                 }
  231.             }
  232.         }
  233.         /// <summary>
  234.         /// 执行查询语句,返回SQLiteDataReader
  235.         /// </summary>
  236.         /// <param name="strSQL">查询语句</param>
  237.         /// <returns>SQLiteDataReader</returns>
  238.         public static SQLiteDataReader ExecuteReader(string strSQL)
  239.         {
  240.             SQLiteConnection connection = new SQLiteConnection(connectionString);
  241.             SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
  242.             try
  243.             {
  244.                 connection.Open();
  245.                 SQLiteDataReader myReader = cmd.ExecuteReader();
  246.                 return myReader;
  247.             }
  248.             catch (System.Data.SQLite.SQLiteException e)
  249.             {
  250.                 throw new Exception(e.Message);
  251.             }
  252.  
  253.         }
  254.         /// <summary>
  255.         /// 执行查询语句,返回DataSet
  256.         /// </summary>
  257.         /// <param name="SQLString">查询语句</param>
  258.         /// <returns>DataSet</returns>
  259.         public static DataSet Query(string SQLString)
  260.         {
  261.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  262.             {
  263.                 DataSet ds = new DataSet();
  264.                 try
  265.                 {
  266.                     connection.Open();
  267.                     SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
  268.                     command.Fill(ds, "ds");
  269.                 }
  270.                 catch (System.Data.SQLite.SQLiteException ex)
  271.                 {
  272.                     throw new Exception(ex.Message);
  273.                 }
  274.                 return ds;
  275.             }
  276.         }
  277.  
  278.  
  279.         #endregion
  280.  
  281.         #region 执行带参数的SQL语句
  282.  
  283.         /// <summary>
  284.         /// 执行SQL语句,返回影响的记录数
  285.         /// </summary>
  286.         /// <param name="SQLString">SQL语句</param>
  287.         /// <returns>影响的记录数</returns>
  288.         public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms)
  289.         {
  290.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  291.             {
  292.                 using (SQLiteCommand cmd = new SQLiteCommand())
  293.                 {
  294.                     try
  295.                     {
  296.                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  297.                         int rows = cmd.ExecuteNonQuery();
  298.                         cmd.Parameters.Clear();
  299.                         return rows;
  300.                     }
  301.                     catch (System.Data.SQLite.SQLiteException E)
  302.                     {
  303.                         throw new Exception(E.Message);
  304.                     }
  305.                 }
  306.             }
  307.         }
  308.  
  309.  
  310.         /// <summary>
  311.         /// 执行多条SQL语句,实现数据库事务。
  312.         /// </summary>
  313.         /// <param name="SQLStringList">SQL语句的哈希表(key为sql语句,value是该语句的SQLiteParameter[])</param>
  314.         public static void ExecuteSqlTran(Hashtable SQLStringList)
  315.         {
  316.             using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  317.             {
  318.                 conn.Open();
  319.                 using (SQLiteTransaction trans = conn.BeginTransaction())
  320.                 {
  321.                     SQLiteCommand cmd = new SQLiteCommand();
  322.                     try
  323.                     {
  324.                         //循环
  325.                         foreach (DictionaryEntry myDE in SQLStringList)
  326.                         {
  327.                             string cmdText = myDE.Key.ToString();
  328.                             SQLiteParameter[] cmdParms = (SQLiteParameter[])myDE.Value;
  329.                             PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
  330.                             int val = cmd.ExecuteNonQuery();
  331.                             cmd.Parameters.Clear();
  332.  
  333.                             trans.Commit();
  334.                         }
  335.                     }
  336.                     catch
  337.                     {
  338.                         trans.Rollback();
  339.                         throw;
  340.                     }
  341.                 }
  342.             }
  343.         }
  344.  
  345.  
  346.         /// <summary>
  347.         /// 执行一条计算查询结果语句,返回查询结果(object)。
  348.         /// </summary>
  349.         /// <param name="SQLString">计算查询结果语句</param>
  350.         /// <returns>查询结果(object)</returns>
  351.         public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms)
  352.         {
  353.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  354.             {
  355.                 using (SQLiteCommand cmd = new SQLiteCommand())
  356.                 {
  357.                     try
  358.                     {
  359.                         PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  360.                         object obj = cmd.ExecuteScalar();
  361.                         cmd.Parameters.Clear();
  362.                         if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
  363.                         {
  364.                             return null;
  365.                         }
  366.                         else
  367.                         {
  368.                             return obj;
  369.                         }
  370.                     }
  371.                     catch (System.Data.SQLite.SQLiteException e)
  372.                     {
  373.                         throw new Exception(e.Message);
  374.                     }
  375.                 }
  376.             }
  377.         }
  378.  
  379.         /// <summary>
  380.         /// 执行查询语句,返回SQLiteDataReader
  381.         /// </summary>
  382.         /// <param name="strSQL">查询语句</param>
  383.         /// <returns>SQLiteDataReader</returns>
  384.         public static SQLiteDataReader ExecuteReader(string SQLString, params SQLiteParameter[] cmdParms)
  385.         {
  386.             SQLiteConnection connection = new SQLiteConnection(connectionString);
  387.             SQLiteCommand cmd = new SQLiteCommand();
  388.             try
  389.             {
  390.                 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  391.                 SQLiteDataReader myReader = cmd.ExecuteReader();
  392.                 cmd.Parameters.Clear();
  393.                 return myReader;
  394.             }
  395.             catch (System.Data.SQLite.SQLiteException e)
  396.             {
  397.                 throw new Exception(e.Message);
  398.             }
  399.  
  400.         }
  401.  
  402.         /// <summary>
  403.         /// 执行查询语句,返回DataSet
  404.         /// </summary>
  405.         /// <param name="SQLString">查询语句</param>
  406.         /// <returns>DataSet</returns>
  407.         public static DataSet Query(string SQLString, params SQLiteParameter[] cmdParms)
  408.         {
  409.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  410.             {
  411.                 SQLiteCommand cmd = new SQLiteCommand();
  412.                 PrepareCommand(cmd, connection, null, SQLString, cmdParms);
  413.                 using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
  414.                 {
  415.                     DataSet ds = new DataSet();
  416.                     try
  417.                     {
  418.                         da.Fill(ds, "ds");
  419.                         cmd.Parameters.Clear();
  420.                     }
  421.                     catch (System.Data.SQLite.SQLiteException ex)
  422.                     {
  423.                         throw new Exception(ex.Message);
  424.                     }
  425.                     return ds;
  426.                 }
  427.             }
  428.         }
  429.  
  430.  
  431.         private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, SQLiteParameter[] cmdParms)
  432.         {
  433.             if (conn.State != ConnectionState.Open)
  434.                 conn.Open();
  435.             cmd.Connection = conn;
  436.             cmd.CommandText = cmdText;
  437.             if (trans != null)
  438.                 cmd.Transaction = trans;
  439.             cmd.CommandType = CommandType.Text;//cmdType;
  440.             if (cmdParms != null)
  441.             {
  442.                 foreach (SQLiteParameter parm in cmdParms)
  443.                     cmd.Parameters.Add(parm);
  444.             }
  445.         }
  446.  
  447.         #endregion
  448.     }
  449. }
  450. //csharp/6835

回复 "C#封装的Sqlite访问类"

这儿你可以回复上面这条便签

captcha