hisence-yolo-detection/HisenceYoloDetection/SQLiteHelper.cs

640 lines
22 KiB
C#
Raw Permalink Normal View History

2024-05-31 10:14:57 +08:00
using System;
using System.Collections;
using System.Collections.Specialized;
using System.Data;
using System.Data.SQLite;
using System.Configuration;
2024-06-11 11:40:47 +08:00
using System.Data.SqlClient;
using System.Data.SqlTypes;
2024-05-31 10:14:57 +08:00
public class SQLiteHelper
{
//<2F><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
// public static string connectionString = ConfigurationManager.ConnectionStrings["ProcessDB"].ConnectionString;
public static string connectionString = "Data Source = D:\\Hisence\\DataBase\\ProcessDB.db;BinaryGUID=False;";
// public SQLiteHelper() { }
#region <EFBFBD><EFBFBD><EFBFBD>÷<EFBFBD><EFBFBD><EFBFBD>
public static int GetMaxID(string FieldName, string TableName)
{
string strsql = "select max(" + FieldName + ")+1 from " + TableName;
object obj = GetSingle(strsql);
if (obj == null)
{
return 1;
}
else
{
return int.Parse(obj.ToString());
}
}
public static bool Exists(string strSql)
{
object obj = GetSingle(strSql);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
public static bool Exists(string strSql, params SQLiteParameter[] cmdParms)
{
object obj = GetSingle(strSql, cmdParms);
int cmdresult;
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
cmdresult = 0;
}
else
{
cmdresult = int.Parse(obj.ToString());
}
if (cmdresult == 0)
{
return false;
}
else
{
return true;
}
}
#endregion
#region ִ<EFBFBD>м<EFBFBD><EFBFBD><EFBFBD>SQL<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// <summary>
/// ִ<><D6B4>SQL<51><4C><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD>Ӱ<EFBFBD><D3B0><EFBFBD>ļ<EFBFBD>¼<EFBFBD><C2BC>
/// </summary>
/// <param name="SQLString">SQL<51><4C><EFBFBD><EFBFBD></param>
/// <returns>Ӱ<><D3B0><EFBFBD>ļ<EFBFBD>¼<EFBFBD><C2BC></returns>
public static int ExecuteSql(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// ִ<><D6B4>SQL<51><4C><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>еȴ<D0B5>ʱ<EFBFBD><CAB1>
/// </summary>
/// <param name="SQLString"></param>
/// <param name="Times"></param>
/// <returns></returns>
public static int ExecuteSqlByTime(string SQLString, int Times)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
cmd.CommandTimeout = Times;
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
connection.Close();
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// ִ<>ж<EFBFBD><D0B6><EFBFBD>SQL<51><4C><EFBFBD>䣬ʵ<E4A3AC><CAB5><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="SQLStringList"><3E><><EFBFBD><EFBFBD>SQL<51><4C><EFBFBD><EFBFBD></param>
public static bool ExecuteSqlTran(ArrayList SQLStringList)
{
bool isSuccess = false;
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
SQLiteCommand cmd = new SQLiteCommand();
cmd.Connection = conn;
cmd.CommandText = "PRAGMA synchronous = OFF;";
cmd.ExecuteNonQuery();
SQLiteTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;
try
{
for (int n = 0; n < SQLStringList.Count; n++)
{
string strsql = SQLStringList[n].ToString();
if (strsql.Trim().Length > 1)
{
cmd.CommandText = strsql;
cmd.ExecuteNonQuery();
}
}
tx.Commit();
isSuccess = true;
}
catch (System.Data.SQLite.SQLiteException E)
{
tx.Rollback();
isSuccess = false;
throw new Exception(E.Message);
}
}
return isSuccess;
}
/// <summary>
/// ִ<>д<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><E6B4A2><EFBFBD>̲<EFBFBD><CCB2><EFBFBD><EFBFBD>ĵ<EFBFBD>SQL<51><4C><EFBFBD>
/// </summary>
/// <param name="SQLString">SQL<51><4C><EFBFBD><EFBFBD></param>
/// <param name="content"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֶ<EFBFBD><D6B6>Ǹ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD></param>
/// <returns>Ӱ<><D3B0><EFBFBD>ļ<EFBFBD>¼<EFBFBD><C2BC></returns>
public static int ExecuteSql(string SQLString, string content)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// ִ<>д<EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><E6B4A2><EFBFBD>̲<EFBFBD><CCB2><EFBFBD><EFBFBD>ĵ<EFBFBD>SQL<51><4C><EFBFBD>
/// </summary>
/// <param name="SQLString">SQL<51><4C><EFBFBD><EFBFBD></param>
/// <param name="content"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD>ֶ<EFBFBD><D6B6>Ǹ<EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD><EFBFBD>£<EFBFBD><C2A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><C5A3><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD></param>
/// <returns>Ӱ<><D3B0><EFBFBD>ļ<EFBFBD>¼<EFBFBD><C2BC></returns>
public static object ExecuteSqlGet(string SQLString, string content)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
SQLiteCommand cmd = new SQLiteCommand(SQLString, connection);
SQLiteParameter myParameter = new SQLiteParameter("@content", DbType.String);
myParameter.Value = content;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// <20><><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD>ֶ<EFBFBD>(<28><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƶ<EFBFBD><C6B5><EFBFBD>һ<EFBFBD><D2BB>ʵ<EFBFBD><CAB5>)
/// </summary>
/// <param name="strSQL">SQL<51><4C><EFBFBD><EFBFBD></param>
/// <param name="fs">ͼ<><CDBC><EFBFBD>ֽ<EFBFBD>,<2C><><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD>ֶ<EFBFBD><D6B6><EFBFBD><EFBFBD><EFBFBD>Ϊimage<67><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <returns>Ӱ<><D3B0><EFBFBD>ļ<EFBFBD>¼<EFBFBD><C2BC></returns>
public static int ExecuteSqlInsertImg(string strSQL, byte[] fs)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
SQLiteParameter myParameter = new SQLiteParameter("@fs", DbType.Binary);
myParameter.Value = fs;
cmd.Parameters.Add(myParameter);
try
{
connection.Open();
int rows = cmd.ExecuteNonQuery();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
finally
{
cmd.Dispose();
connection.Close();
}
}
}
/// <summary>
/// ִ<><D6B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E4A3AC><EFBFBD>ز<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>object<63><74><EFBFBD><EFBFBD>
/// </summary>
/// <param name="SQLString"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <returns><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>object<63><74></returns>
public static object GetSingle(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SQLite.SQLiteException e)
{
connection.Close();
throw new Exception(e.Message);
}
}
}
}
/// <summary>
/// ִ<>в<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD>SQLiteDataReader(ʹ<>ø÷<C3B8><C3B7><EFBFBD><EFBFBD>м<EFBFBD>Ҫ<EFBFBD>ֹ<EFBFBD><D6B9>ر<EFBFBD>SQLiteDataReader<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
/// </summary>
/// <param name="strSQL"><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD></param>
/// <returns>SQLiteDataReader</returns>
public static SQLiteDataReader ExecuteReader(string strSQL)
{
SQLiteConnection connection = new SQLiteConnection(connectionString);
SQLiteCommand cmd = new SQLiteCommand(strSQL, connection);
try
{
connection.Open();
SQLiteDataReader myReader = cmd.ExecuteReader();
return myReader;
}
catch (System.Data.SQLite.SQLiteException e)
{
throw new Exception(e.Message);
}
//finally //<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ˹رգ<D8B1><D5A3><EFBFBD><EFBFBD>򣬷<EFBFBD><F2A3ACB7>صĶ<D8B5><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD>ʹ<EFBFBD><CAB9>
//{
// cmd.Dispose();
// connection.Close();
//}
}
/// <summary>
/// ִ<>в<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD>DataSet
/// </summary>
/// <param name="SQLString"><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD></param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.Fill(ds, "ds");
connection.Close();
command.Dispose();
}
catch (System.Data.SQLite.SQLiteException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
2024-06-11 11:40:47 +08:00
// ִ<><D6B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD>һ<E4B5BD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD>
public static List<T> ExecuteQuery<T>(string SQLString, Func<IDataRecord, T> selector, params SQLiteParameter[] parameters)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
{
// <20><><EFBFBD>Ӳ<EFBFBD><D3B2><EFBFBD>
cmd.Parameters.AddRange(parameters);
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
connection.Open();
// <20><><EFBFBD><EFBFBD>DataReader<65><72><EFBFBD>󲢶<EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><E4B5BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӵ<EFBFBD><D3B5>б<EFBFBD><D0B1><EFBFBD>
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
List<T> list = new List<T>();
while (reader.Read())
{
list.Add(selector(reader));
}
return list;
}
}
}
}
2024-05-31 10:14:57 +08:00
2024-06-11 11:40:47 +08:00
public static DataSet Query(string SQLString, string TableName)
2024-05-31 10:14:57 +08:00
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.Fill(ds, TableName);
}
catch (System.Data.SQLite.SQLiteException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
/// <summary>
/// ִ<>в<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD>DataSet,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ִ<EFBFBD>еȴ<D0B5>ʱ<EFBFBD><CAB1>
/// </summary>
/// <param name="SQLString"></param>
/// <param name="Times"></param>
/// <returns></returns>
public static DataSet Query(string SQLString, int Times)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
DataSet ds = new DataSet();
try
{
connection.Open();
SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
command.SelectCommand.CommandTimeout = Times;
command.Fill(ds, "ds");
}
catch (System.Data.SQLite.SQLiteException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
#endregion
#region ִ<EFBFBD>д<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SQL<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// <summary>
/// ִ<><D6B4>SQL<51><4C><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD>Ӱ<EFBFBD><D3B0><EFBFBD>ļ<EFBFBD>¼<EFBFBD><C2BC>
/// </summary>
/// <param name="SQLString">SQL<51><4C><EFBFBD><EFBFBD></param>
/// <returns>Ӱ<><D3B0><EFBFBD>ļ<EFBFBD>¼<EFBFBD><C2BC></returns>
public static int ExecuteSql(string SQLString, params SQLiteParameter[] cmdParms)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
int rows = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return rows;
}
catch (System.Data.SQLite.SQLiteException E)
{
throw new Exception(E.Message);
}
}
}
}
/// <summary>
/// ִ<>ж<EFBFBD><D0B6><EFBFBD>SQL<51><4C><EFBFBD>䣬ʵ<E4A3AC><CAB5><EFBFBD><EFBFBD><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
/// </summary>
/// <param name="SQLStringList">SQL<51><4C><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD>ϣ<EFBFBD><CFA3><EFBFBD><EFBFBD>keyΪsql<71><6C><EFBFBD>䣬value<75>Ǹ<EFBFBD><C7B8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>SQLiteParameter[]<5D><></param>
public static void ExecuteSqlTran(Hashtable SQLStringList)
{
using (SQLiteConnection conn = new SQLiteConnection(connectionString))
{
conn.Open();
using (SQLiteTransaction trans = conn.BeginTransaction())
{
SQLiteCommand cmd = new SQLiteCommand();
try
{
//ѭ<><D1AD>
foreach (DictionaryEntry myDE in SQLStringList)
{
string cmdText = myDE.Key.ToString();
SQLiteParameter[] cmdParms = (SQLiteParameter[])myDE.Value;
PrepareCommand(cmd, conn, trans, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
trans.Commit();
}
catch (System.Data.SQLite.SQLiteException e)
{
trans.Rollback();
throw new Exception(e.Message);
}
}
}
}
/// <summary>
/// ִ<><D6B4>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E4A3AC><EFBFBD>ز<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>object<63><74><EFBFBD><EFBFBD>
/// </summary>
/// <param name="SQLString"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <returns><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD>object<63><74></returns>
public static object GetSingle(string SQLString, params SQLiteParameter[] cmdParms)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
{
return null;
}
else
{
return obj;
}
}
catch (System.Data.SQLite.SQLiteException e)
{
throw new Exception(e.Message);
}
}
}
}
/// <summary>
/// ִ<>в<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD>SQLiteDataReader (ʹ<>ø÷<C3B8><C3B7><EFBFBD><EFBFBD>м<EFBFBD>Ҫ<EFBFBD>ֹ<EFBFBD><D6B9>ر<EFBFBD>SQLiteDataReader<65><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD>)
/// </summary>
/// <param name="strSQL"><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD></param>
/// <returns>SQLiteDataReader</returns>
public static SQLiteDataReader ExecuteReader(string SQLString, params SQLiteParameter[] cmdParms)
{
SQLiteConnection connection = new SQLiteConnection(connectionString);
SQLiteCommand cmd = new SQLiteCommand();
try
{
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
SQLiteDataReader myReader = cmd.ExecuteReader();
cmd.Parameters.Clear();
return myReader;
}
catch (System.Data.SQLite.SQLiteException e)
{
throw new Exception(e.Message);
}
//finally //<2F><><EFBFBD><EFBFBD><EFBFBD>ڴ˹رգ<D8B1><D5A3><EFBFBD><EFBFBD>򣬷<EFBFBD><F2A3ACB7>صĶ<D8B5><C4B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>޷<EFBFBD>ʹ<EFBFBD><CAB9>
//{
// cmd.Dispose();
// connection.Close();
//}
}
/// <summary>
/// ִ<>в<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><E4A3AC><EFBFBD><EFBFBD>DataSet
/// </summary>
/// <param name="SQLString"><3E><>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD></param>
/// <returns>DataSet</returns>
public static DataSet Query(string SQLString, params SQLiteParameter[] cmdParms)
{
using (SQLiteConnection connection = new SQLiteConnection(connectionString))
{
SQLiteCommand cmd = new SQLiteCommand();
PrepareCommand(cmd, connection, null, SQLString, cmdParms);
using (SQLiteDataAdapter da = new SQLiteDataAdapter(cmd))
{
DataSet ds = new DataSet();
try
{
da.Fill(ds, "ds");
cmd.Parameters.Clear();
}
catch (System.Data.SQLite.SQLiteException ex)
{
throw new Exception(ex.Message);
}
return ds;
}
}
}
public static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn,
SQLiteTransaction trans, string cmdText, SQLiteParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = CommandType.Text;//cmdType;
if (cmdParms != null)
{
foreach (SQLiteParameter parameter in cmdParms)
{
if ((parameter.Direction == ParameterDirection.InputOutput
|| parameter.Direction == ParameterDirection.Input) &&
(parameter.Value == null))
{
parameter.Value = DBNull.Value;
}
cmd.Parameters.Add(parameter);
}
}
}
#endregion
#region <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ת<EFBFBD><EFBFBD>
/// <summary>
/// <20>Ż<EFBFBD>һ<EFBFBD><D2BB>SQLiteParameter
/// </summary>
/// <param name="name"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="type"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
/// <param name="size"><3E><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>С</param>
/// <param name="value"><3E><><EFBFBD><EFBFBD>ֵ</param>
/// <returns>SQLiteParameter<65><72>ֵ</returns>
public static SQLiteParameter MakeSQLiteParameter(string name,
DbType type, int size, object value)
{
SQLiteParameter parm = new SQLiteParameter(name, type, size);
parm.Value = value;
return parm;
}
public static SQLiteParameter MakeSQLiteParameter(string name, DbType type, object value)
{
SQLiteParameter parm = new SQLiteParameter(name, type);
parm.Value = value;
return parm;
}
#endregion
}