C # SQLite database class,

Source: Internet
Author: User
C # The class used to operate the SQLite database. It is absolutely original at a.m.,

Using system;
Using system. text;
Using system. Data;
Using system. Data. SQLite;

Namespace WQ. SQLite. Database
{

// If not, contact the developer QQ: 13164946

// Author: epe521 EML: epe521@qq.com

Public class sqlitedatabase: idisposable
{
Sqliteconnection conn;
Private int itimeout = 30;
Private string strerro;
Private string connstr;

Public sqlitedatabase (string _ connstring)
{
Connstr = _ connstring;
}

Private sqlitecommand createcommand (string sqlstring)
{
This. Open (connstring );
Sqlitecommand command = new sqlitecommand (sqlstring, this. Conn );
Command. commandtimeout = This. Timeout;
Command. commandtype = commandtype. text;
Command. commandtext = sqlstring;
Return command;
}

Public bool excquery (string sqlstring)
{
Try
{
Sqlitecommand command = new sqlitecommand (sqlstring, new sqliteconnection (connstring ));
Command. Connection. open ();
Command. executenonquery ();
Command. Connection. Close ();
Return true;
}
Catch (exception)
{
This. strerro = Exception. tostring ();
Return false;
}
}

Public datatable getdatatable (string sqlstring)
{
Try
{
Sqliteconnection selectconnection = new sqliteconnection (connstring );
Sqlitedataadapter adapter = new sqlitedataadapter (sqlstring, selectconnection );
Dataset dataset = new dataset ();
Adapter. Fill (dataset, "mytable ");
Return dataset. Tables ["mytable"];
}
Catch (exception)
{
This. strerro = sqlstring + "\ n" + exception. message;
Return NULL;
}
}

Public bool getdatatable (string sqlstring, ref datatable)
{
Try
{
Sqliteconnection selectconnection = new sqliteconnection (connstring );
Sqlitedataadapter adapter = new sqlitedataadapter (sqlstring, selectconnection );
Dataset dataset = new dataset ();
Adapter. Fill (dataset, "mytable ");
Datatable = dataset. Tables ["mytable"];
Return true;
}
Catch (exception)
{
This. strerro = sqlstring + "\ n" + exception. message;
Return false;
}
}

Public bool getdatatable (string sqlstring, sqliteparameter [] Pa, ref datatable DT)
{
Try
{
Sqliteconnection selectconnection = new sqliteconnection (connstring );
Sqlitedataadapter adapter = new sqlitedataadapter (sqlstring, selectconnection );
Dataset dataset = new dataset ();
For (INT I = 0; I <Pa. length; I ++)
{
Adapter. selectcommand. Parameters. Add (PA [I]);
}
Adapter. Fill (dataset, "mytable ");
Dt = dataset. Tables ["mytable"];
Return true;
}
Catch (exception)
{
This. strerro = sqlstring + "\ n" + exception. message;
Return false;
}
}

Public bool getdatatable (string sqlstring, int pageindex, int maxrecords, ref datatable)
{
Try
{
Sqliteconnection selectconnection = new sqliteconnection (connstring );
Sqlitedataadapter adapter = new sqlitedataadapter (sqlstring, selectconnection );
Dataset dataset = new dataset ();
Adapter. Fill (dataset, pageindex, maxrecords, "mytable ");
Datatable = dataset. Tables ["mytable"];
Return true;
}
Catch (exception)
{
This. strerro = sqlstring + "\ n" + exception. message;
Return false;
}
}

Public String getonevalue (string sqlstring, sqliteparameter [] Pa)
{
Sqlitecommand command = new sqlitecommand (sqlstring, new sqliteconnection (connstring ));
For (INT I = 0; I <Pa. length; I ++)
{
Command. Parameters. Add (PA [I]);
}
Command. Connection. open ();
String STR = command. executescalar (). tostring ();
Command. Connection. Close ();
Return STR;
}

Public String getonevalue (string connstring, string sqlstring)
{
Sqlitecommand command = This. createcommand (sqlstring );
String STR = command. executescalar (). tostring ();
Command. Connection. Close ();
Return STR;
}

Private void open (string _ connectionstring)
{
If (this. Conn = NULL)
{
This. Conn = new sqliteconnection (_ connectionstring );
This. Conn. open ();
}
Else if (this. Conn. State! = Connectionstate. open)
{
This. Conn. open ();
}
}

Public void close ()
{
If (this. Conn! = NULL)
{
This. Conn. Close ();
}
Else if (this. Conn. State = connectionstate. open)
{
This. Conn. Close ();
}
}

Public void dispose ()
{< br> If (this. Conn! = NULL)
{< br> This. Conn. Dispose ();
This. Conn = NULL;
}< BR >}

Public String errstring
{< br> Get
{< br> return this. strerro;
}< BR >}

private string connstring
{< br> Get
{< br> return this. connstr;
}< br> set
{< br> This. connstr = value;
}< BR >}

Public int timeout
{
Get
{
Return this. itimeout;
}
Set
{
This. itimeout = value;
}
}

 

}
}

There are also SQL operations, which are absolutely powerful. Hi me if necessary

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.