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;
}