Home > database >  SQLiteHelper
SQLiteHelper

Time:10-09

 
using System;
Using System. The Data;
Using System. Text. RegularExpressions;
Using System. Xml;
Using System. IO;
using System.Collections;
Using System. Data. SQLite;
Using System. The Configuration;//add the.net reference
The namespace Utills
{
///& lt; Summary>
///class of SQLite operation
///reference: System. Data. SQLite. DLL [version: 3.6.23.1 (original name: SQlite3. DLL)]
///& lt;/summary>
Public class SQLiteHelper
{
///& lt; Summary>
///all member functions are static, the constructor defined as private
///& lt;/summary>
Private SQLiteHelper ()
{
}
///& lt; Summary>
///the connection string
///& lt;/summary>
Public static string ConnectionString
{//"Data Source=Test. Db3; Pooling=true; FailIfMissing=false ";
The get
{
////(" SQLiteConnectionString ") under the AppSettings node
String text=ConfigurationManager. AppSettings [" SQLiteConnectionString "];
//string str2=ConfigurationManager. AppSettings [" IsEncrypt "];
//if (str2=="true")
//{
//text=DesEncrypt Decrypt (text);
//}
Return the text;
}
}
Private static SQLiteConnection _Conn=null;
///& lt; Summary>
///connection object
///& lt;/summary>
Public static SQLiteConnection Conn
{
The get
{
If (_Conn==null) _Conn=new SQLiteConnection (the ConnectionString);
Return SQLiteHelper. _Conn;
}
The set {SQLiteHelper. _Conn=value; }
}


# region CreateCommand (commandText, SQLiteParameter [])
///& lt; Summary>
///create a command
///& lt;/summary>
///& lt; Param name="connection" & gt; The connection & lt;/param>
///& lt; Param name="commandText & gt;" The statement & lt;/param>
///& lt; Param name="commandParameters & gt;" Statement parameters. & lt;/param>
///& lt; Returns> SQLite Command
Public static SQLiteCommand CreateCommand (string commandText, params SQLiteParameter [] commandParameters)
{
SQLiteCommand CMD=new SQLiteCommand (commandText, Conn);
If (commandParameters. Length & gt; 0)
{
Foreach (SQLiteParameter parm in commandParameters)
CMD. The Parameters. The Add (parm);
}
Return CMD.
}
# endregion


# region CreateParameter (parameterName parameterType, parameterValue)
///& lt; Summary>
///create the parameter
///& lt;/summary>
///& lt; Param name="parameterName" & gt; Parameter name & lt;/param>
///& lt; Param name="parameterType & gt;" The parameter types & lt;/param>
///& lt; Param name="parameterValue & gt;" The parameter value & lt;/param>
///& lt; Returns> The parameters of return to create & lt;/returns>
Public static SQLiteParameter CreateParameter (string parameterName, System. Data. The DbType parameterType, object parameterValue)
{
SQLiteParameter parameter=new SQLiteParameter ();
The parameter. The DbType=parameterType;
The parameter. The ParameterName=ParameterName;
The parameter Value=https://bbs.csdn.net/topics/parameterValue;
The return parameter.
}
# endregion

# region ExecuteDataSet (commandText, paramList [])
///& lt; Summary>
///
query dataset///& lt;/summary>
///& lt; Param name="cn" & gt; Connection. & lt;/param>
///& lt; Param name="commandText & gt;" Query. & lt;/param>
///& lt; Param name="paramList & gt;" The object parameter list. & lt;/param>
///& lt; Returns>
Public static DataSet ExecuteDataSet (string commandText, params object [] paramList)
{

SQLiteCommand CMD=Conn. CreateCommand ();
Cmd.Com mandText=commandText;
If (paramList!=null)
{
AttachParameters (CMD, commandText, paramList);
}
The DataSet ds=new DataSet ();
If (Conn. State==ConnectionState. Closed)
Conn. The Open ();
SQLiteDataAdapter da=new SQLiteDataAdapter (CMD);
Da. The Fill (ds);
Da. The Dispose ();
CMD. The Dispose ();
Conn. Close ();
Return the ds;
}
# endregion

# region ExecuteDataSet (SQLiteCommand)
///& lt; Summary>
///
query dataset///& lt;/summary>
///& lt; Param name="CMD" & gt; SQLiteCommand object & lt;/param>
///& lt; Returns> Return data set & lt;/returns>
Public static DataSet ExecuteDataSet SQLiteCommand (CMD)
{
If (CMD) Connection. The State==ConnectionState. Closed)
CMD. Connection. The Open ();
The DataSet ds=new DataSet ();
SQLiteDataAdapter da=new SQLiteDataAdapter (CMD);
Da. The Fill (ds);
Da. The Dispose ();
CMD. Connection. The Close ();
CMD. The Dispose ();
Return the ds;
}
# endregion

# region ExecuteDataSet (SQLiteTransaction commandText, params SQLiteParameter [])
///& lt; Summary>
///
query dataset///& lt;/summary>
///& lt; Param name="transaction" & gt; SQLiteTransaction object. & lt;/param>
///& lt; Param name="commandText & gt;" Query. & lt;/param>
///& lt; Param name="commandParameters & gt;" The parameter list of commands. & lt;/param>
///& lt; Returns> DataSet
///& lt; Remarks> Must be performed manually close the connection transaction. Connection. Close
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related