1. 程式人生 > >SQLite資料庫的SQLiteHelper幫助類

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();

        /// <summary> 
        /// 對SQLite資料庫執行增刪改操作,返回受影響的行數。 
        /// </summary> 
        /// <param name="sql">要執行的增刪改的SQL語句</param> 
        /// <param name="parameters">執行增刪改語句的引數</param> 
        /// <returns></returns> 
        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;
        }
        /// <summary> 
        /// 執行一個查詢語句,返回SQLiteDataReader例項 
        /// </summary> 
        /// <param name="sql">要執行的查詢語句</param> 
        /// <param name="parameters">執行查詢語句的引數</param> 
        /// <returns></returns> 
        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);
        }
        /// <summary> 
        /// 執行一個查詢語句,返回一個包含查詢結果的DataTable 
        /// </summary> 
        /// <param name="sql">要執行的查詢語句</param> 
        /// <param name="parameters">執行查詢語句的引數</param> 
        /// <returns></returns> 
        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;
                }
            }
        }
        /// <summary> 
        /// 執行一個查詢語句,返回查詢結果的第一行第一列 
        /// </summary> 
        /// <param name="sql">要執行的查詢語句</param> 
        /// <param name="parameters">執行SQL查詢語句所需要的引數,引數必須以它們在SQL語句中的順序為準</param> 
        /// <returns></returns> 
        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資料庫必須的檔案下載地址