Sqlitehelp is similar to sqlhelp

Source: Internet
Author: User

Class c_sqlite
{
/// <Summary>
/// Obtain the connection object
/// </Summary>
/// <Returns> </returns>
Public static sqliteconnection getsqliteconnection ()
{
Return new sqliteconnection (@ "Data Source = data. DB ");
}

/// <Summary>
/// Modify data table records
/// </Summary>
/// <Returns> </returns>
Public bool updatetable (datatable srctable, string tablename)
{
Bool isok = false;
Try
{
Sqlitecommand command = new sqlitecommand ();
Command. commandtext = "select * from" + tablename;
Sqlitedataadapter sqliteda = new sqlitedataadapter (command );
Sqlitecommandbuilder sqlitecb = new sqlitecommandbuilder (sqliteda );
Sqliteda. insertcommand = sqlitecb. getinsertcommand ();
Sqliteda. deletecommand = sqlitecb. getdeletecommand ();
Sqliteda. updatecommand = sqlitecb. getupdatecommand ();
Sqliteda. Update (srctable );

Isok = true;
}
Catch
{;}
Return isok;
}
/// <Summary>
/// Read the returned data table
/// </Summary>
Public static datatable executedatatable (string sqlstr)
{
Datatable = new datatable ("mytabe ");
Sqlitecommand command = new sqlitecommand ();
Using (sqliteconnection conn = getsqliteconnection ())
{
If (conn. State! = Connectionstate. Open) Conn. open ();
Command. Connection = conn;
Command. commandtext = sqlstr;
Sqlitedataadapter da = new sqlitedataadapter (command );
Da. Fill (datatable );
Conn. Close ();
}
Return datatable;
}

/// <Summary>
/// Return the number of affected rows
/// </Summary>
Public static int updatedata (string sqlstr)
{
Sqlitecommand command = new sqlitecommand ();
Using (sqliteconnection conn = getsqliteconnection ())
{
If (conn. State! = Connectionstate. Open) Conn. open ();
Command. Connection = conn;
Command. commandtext = sqlstr;
Command. executenonquery ();
Conn. Close ();
Return 1;
}
}
// Bind a data table
Public static void bindgrid (datagridview mydatagrid, string sqlstr)
{
Mydatagrid. datasource = executedatatable (sqlstr );
}
// Bind the data drop-down text box
Public static void bindcbox (ComboBox cbox, string sqlstr, string dspyfld)
{
Cbox. datasource = executedatatable (sqlstr );
Cbox. displaymember = dspyber;
}
// Set the column width of the data table
Public static void setgridwidth (datagridview mydatagrid, int [] width)
{
For (INT I = 0; I <width. length; I ++)
Mydatagrid. Columns [I]. width = width [I];
}
// Return the data
Public static string [,] getdata (string sqlstr)
{
Datatable = executedatatable (sqlstr );
String [,] redata = new string [datatable. Rows. Count, datatable. Columns. Count];
For (INT I = 0; I <datatable. Rows. Count; I ++)
{
For (Int J = 0; j <datatable. Columns. Count; j ++)
{
Redata [I, j] = datatable. Rows [I] [J]. tostring ();
}
}
Return redata;
}

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.