C# SQLite通用读写操作方法

        /// 
        /// 执行SQL语句,返回影响的记录数
        /// 
        /// SQL语句
        /// 影响的记录数
        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();
                        }
                    }
                }
            }
        }


        /// 
        /// 执行查询语句,返回DataTable,DataSet的第一张表
        /// 
        /// 查询语句
        /// DataTable
        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();
                }
            }
        }


你可能感兴趣的:(C#,wpf,winform)