Operating class of Self-encapsulated ASP. NET SQLite Database

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.