Code
/*
* Author: Niu Yu
* Creation Time: 10:43:14
* Email: 164423073@qq.com
* Description: The encapsulation class for adding, querying, modifying, and deleting SQLite Databases
*/
UsingSystem;
UsingSystem. Collections. Generic;
UsingSystem. LINQ;
UsingSystem. text;
UsingSystem. Data;
UsingSystem. Data. SQLite;
UsingSystem. Web. configuration;
UsingSystem. Web;
Namespace Niunan. tg029.hr. Utility
{
Public Class Sqlitehelper
{
Private Sqliteconnection Conn = Null ;
Private Sqlitecommand cmd = Null ;
Private Sqlitedatareader SDR = Null ;
Public Sqlitehelper ()
{
// String connstr = webconfigurationmanager. connectionstrings ["connstr"]. tostring ();
String Connstr = " Data Source = " + Httpcontext. Current. server. mappath ( " ~ /Test. SQLite " );
Conn = New Sqliteconnection (connstr );
}
/// <Summary> CREATE command object
///
/// </Summary>
/// <Param name = "SQL"> SQL statement </Param>
Public Void Createcommand ( String SQL ){
Conn. open ();
CMD = New Sqlitecommand (SQL, Conn );
}
/// <Summary> Add Parameters
///
/// </Summary>
/// <Param name = "paramname"> Parameter Name </Param>
/// <Param name = "value"> Value </Param>
Public Void Addparameter ( String Paramname, Object Value ){
Cmd. Parameters. Add ( New Sqliteparameter (paramname, value ));
}
/// <Summary> Execute the SQL statement without adding, deleting, and modifying parameters.
///
/// </Summary>
/// <Param name = "plain text"> Add, delete, and modify SQL statements </Param>
/// <Param name = "CT"> Command type </Param>
/// <Returns> </returns>
Public Bool Executenonquery ()
{
Int Res;
Try
{
Res = Cmd. executenonquery ();
If (Res > 0 )
{
Return True ;
}
}
Catch (Exception ex)
{
Throw Ex;
}
Finally
{
If (Conn. State = Connectionstate. open)
{
Conn. Close ();
}
}
Return False ;
}
/// <Summary> Execute the query SQL statement
///
/// </Summary>
/// <Param name = "plain text"> Query SQL statements </Param>
/// <Returns> </returns>
Public Datatable executequery ()
{
Datatable dt = New Datatable ();
Using (SDR = Cmd. executereader (commandbehavior. closeconnection ))
{
DT. Load (SDR );
}
Return DT;
}
/// <Summary> Returns the value of the first column of the first row of the query result of the SQL statement.
///
/// </Summary>
/// <Returns> </returns>
Public String Executescalar ()
{
String Res = "" ;
Try
{
Object OBJ = Cmd. executescalar ();
If (OBJ ! = Null )
{
Res = OBJ. tostring ();
}
}
Catch (Exception ex)
{
Throw Ex;
}
Finally
{
If (Conn. State = Connectionstate. open)
{
Conn. Close ();
}
}
Return Res;
}
}
}
let's look at the attachment in the example. It's a simple encapsulation. It is recorded here for the sake of time !!!
attachment download: http://niunan.net/download/sqlitetest.7z