using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;
using System.Threading;
using System.Configuration;
namespace Chn.gzGISer.DataBase.SQLite
{
/// <summary>
/// SQL數據庫幫助類
/// 使用時請設置SQL數據庫的物理路徑 屬性 DbPath
/// </summary>
public class SQLiteDataBase : IDataBase<SQLiteDataBase>
{
private SQLiteConnection _connection;
/// <summary>
/// 構造函數
/// </summary>
public SQLiteDataBase()
{
}
/// <summary>
/// 構造函數
/// </summary>
/// <param name="connectionString">連接字符串</param>
public SQLiteDataBase(string connectionString)
{
_connection = new SQLiteConnection(connectionString);
}
/// <summary>
/// 從配置文件中初始化連接字段
/// </summary>
/// <param name="connectionStringName">數據庫連接配置文件(.config)中的數據庫連接字段的標籤名</param>
public void InitSQLiteConnectionFromConfig(string connectionStringName)
{
string connectionString = ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
_connection = new SQLiteConnection(connectionString);
}
/// <summary>
/// 打開Connection連接
/// </summary>
public void EnsureConnectionOpen()
{
var retries = 3;
if (_connection.State == ConnectionState.Open)
{
return;
}
else
{
while (retries >= 0 && _connection.State != ConnectionState.Open)
{
_connection.Open();
retries--;
Thread.Sleep(30);
}
}
}
/// <summary>
/// 關閉Connection連接
/// </summary>
public void EnsureConnectionClose()
{
if (_connection.State == ConnectionState.Open)
{
_connection.Close();
}
}
/// <summary>
/// 釋放Connection連接
/// </summary>
public void Dispose()
{
if (_connection != null)
{
_connection.Dispose();
_connection = null;
}
}
/// <summary>
/// 執行查詢 SQL 語句並返回一個表的數據副本
/// </summary>
/// <param name="commandText">SQL 語句</param>
/// <param name="parameters">SQL 語句參數列表</param>
/// <returns></returns>
public DataTable ExecuteQuery(string commandText, Dictionary<string, object> parameters)
{
if (string.IsNullOrEmpty(commandText))
{
throw new Exception("查詢條件爲空");
}
DataTable result = null;
try
{
EnsureConnectionOpen();
SQLiteCommand command = CreatCommand(commandText, parameters);
using (SQLiteDataAdapter dataAdapter = new SQLiteDataAdapter(command))
{
dataAdapter.Fill(result);
}
}
catch (Exception)
{
throw;
}
finally
{
EnsureConnectionClose();
}
return result;
}
/// <summary>
/// 執行非查詢 SQLiet 語句
/// </summary>
/// <param name="commandText">SQLiet 語句</param>
/// <param name="parameters">SQLiet 語句參數列表</param>
/// <returns>受影響的行數</returns>
public int ExecuteNonQuery(string commandText, Dictionary<string, object> parameters)
{
if (string.IsNullOrEmpty(commandText))
{
throw new Exception("查詢條件爲空");
}
int result = -1;
try
{
EnsureConnectionOpen();
SQLiteCommand command = CreatCommand(commandText, parameters);
result = command.ExecuteNonQuery();
}
catch (Exception)
{
throw;
}
finally
{
EnsureConnectionClose();
}
return result;
}
/// <summary>
/// 執行一個 SQL 語句並返回一個標量
/// </summary>
/// <param name="commandText">SQL 語句</param>
/// <param name="parameters">SQL 語句參數列表</param>
/// <returns></returns>
public object ExecuteScalar(string commandText, Dictionary<string, object> parameters)
{
if (string.IsNullOrEmpty(commandText))
{
throw new Exception("查詢條件爲空");
}
object result = null;
try
{
EnsureConnectionOpen();
SQLiteCommand command = CreatCommand(commandText, parameters);
result = command.ExecuteScalar();
}
catch (Exception)
{
throw;
}
finally
{
EnsureConnectionClose();
}
return result;
}
private SQLiteCommand CreatCommand(string commadText, Dictionary<string, object> parameters)
{
SQLiteCommand command = _connection.CreateCommand();
command.CommandText = commadText;
AddParameters(command, parameters);
return command;
}
private void AddParameters(SQLiteCommand command, Dictionary<string, object> parameters)
{
if (parameters == null)
{
return;
}
foreach (KeyValuePair<string, object> param in parameters)
{
var parameter = command.CreateParameter();
parameter.ParameterName = param.Key;
parameter.Value = param.Value ?? DBNull.Value;
command.Parameters.Add(parameter);
}
}
}
}