SQLite数据库的SQLiteHelper帮助类

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MVCDAL
{
   public static class SQLiteHelper
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["SqliteConn"].ToString();

        ///  
        /// 对SQLite数据库执行增删改操作,返回受影响的行数。 
        ///  
        /// 要执行的增删改的SQL语句 
        /// 执行增删改语句的参数 
        ///  
        public static int ExecuteNonQuery(string sql, params SQLiteParameter[] parameters)
        {
            int affectedRows = 0;
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                connection.Open();
                using (DbTransaction transaction = connection.BeginTransaction())
                {
                    using (SQLiteCommand command = new SQLiteCommand(connection))
                    {
                        command.CommandText = sql;
                        if (parameters != null)
                        {
                            command.Parameters.AddRange(parameters);
                        }
                        affectedRows = command.ExecuteNonQuery();
                    }
                    transaction.Commit();
                }
            }
            return affectedRows;
        }
        ///  
        /// 执行一个查询语句,返回SQLiteDataReader实例 
        ///  
        /// 要执行的查询语句 
        /// 执行查询语句的参数 
        ///  
        public static SQLiteDataReader ExecuteReader(string sql, SQLiteParameter[] parameters)
        {
            SQLiteConnection connection = new SQLiteConnection(connectionString);
            SQLiteCommand command = new SQLiteCommand(sql, connection);
            if (parameters != null)
            {
                command.Parameters.AddRange(parameters);
            }
            connection.Open();
            return command.ExecuteReader(CommandBehavior.CloseConnection);
        }
        ///  
        /// 执行一个查询语句,返回一个包含查询结果的DataTable 
        ///  
        /// 要执行的查询语句 
        /// 执行查询语句的参数 
        ///  
        public static DataTable ExecuteDataTable(string sql, params SQLiteParameter[] parameters)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand(sql, connection))
                {
                    if (parameters != null)
                    {
                        command.Parameters.AddRange(parameters);
                    }
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
                    DataTable data = new DataTable();
                    adapter.Fill(data);
                    return data;
                }
            }
        }
        ///  
        /// 执行一个查询语句,返回查询结果的第一行第一列 
        ///  
        /// 要执行的查询语句 
        /// 执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准 
        ///  
        public static Object ExecuteScalar(string sql, params SQLiteParameter[] parameters)
        {
            using (SQLiteConnection connection = new SQLiteConnection(connectionString))
            {
                using (SQLiteCommand command = new SQLiteCommand(sql, connection))
                {
                    if (parameters != null)
                    {
                        command.Parameters.AddRange(parameters);
                    }
                    SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
                    DataTable data = new DataTable();
                    adapter.Fill(data);
                    return data;
                }
            }
        }
    }
}
使用SQLite数据库必须的 System.Data.SQLite.dll文件下载地址

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