51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

C# SQLite通用读写操作方法

        /// <summary>
        /// 执行SQL语句,返回影响的记录数
        /// </summary>
        /// <param name="SQLString">SQL语句</param>
        /// <returns>影响的记录数</returns>
        public static int ExecuteSql(string SQLString)
        {
            lock (_lock)
            {
                using (SQLiteConnection sqliteConn = new SQLiteConnection(sqlConnectionString))
                {
                    sqliteConn.Open();
                    using (SQLiteCommand cmd = sqliteConn.CreateCommand())
                    {
                        try
                        {
                            cmd.CommandText = SQLString;
                            int rows = cmd.ExecuteNonQuery();
                            return rows;
                        }
                        catch (SQLiteException ex)
                        {
                            throw ex;
                        }
                        finally
                        {
                            sqliteConn.Close();
                        }
                    }
                }
            }
        }

        /// &lt;summary&gt;
        /// 执行查询语句,返回DataTable,DataSet的第一张表
        /// &lt;/summary&gt;
        /// &lt;param name="SQLString"&gt;查询语句&lt;/param&gt;
        /// &lt;returns&gt;DataTable&lt;/returns&gt;
        public static DataTable Query(string SQLString)
        {
            using (SQLiteConnection sqliteConn = new SQLiteConnection(sqlConnectionString))
            {
                try
                {
                    sqliteConn.Open();
                    DataSet ds = new DataSet();
                    SQLiteDataAdapter sqliteDa = new SQLiteDataAdapter(SQLString, sqliteConn);
                    sqliteDa.Fill(ds);
                    return ds.Tables[0];
                }
                catch (SQLiteException ex)
                {
                    throw ex;
                }
                finally
                {
                    sqliteConn.Close();
                }
            }
        }</code></pre>



 
<br />



 
<br />



 
<br />



 
<br />



 
<br />



 
<br />



 
<br />



 
​

赞(0)
未经允许不得转载:工具盒子 » C# SQLite通用读写操作方法