First, sqlite operation, direct code, very simple://Create a database file
String Datasource=application.startuppath + "\\test.db";
System.Data.SQLite.SQLiteConnection.CreateFile (DataSource);
Connecting to a database
System.Data.SQLite.SQLiteConnection conn =
New System.Data.SQLite.SQLiteConnection ();
System.Data.SQLite.SQLiteConnectionStringBuilder connstr =
New System.Data.SQLite.SQLiteConnectionStringBuilder ();
ConnStr. DataSource = DataSource;
ConnStr. Password = "admin";//Set password, SQLite ADO. NET implements the database password protection
Conn. ConnectionString = ConnStr. ToString ();
Conn. Open ();
Create a table
System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand ();
String sql = "CREATE TABLE Test (username varchar (), password varchar (20))";
Cmd.commandtext=sql;
Cmd. Connection=conn;
Cmd. ExecuteNonQuery ();
Inserting data
sql = "INSERT into Test VALUES (' Dotnetthink ', ' mypassword ')";
Cmd.commandtext = SQL;
Cmd. ExecuteNonQuery ();
Remove data
sql = "SELECT * from Test";
Cmd.commandtext = SQL;
System.Data.SQLite.SQLiteDataReader reader = cmd. ExecuteReader ();
StringBuilder sb = new StringBuilder ();
while (reader. Read ())
{
Sb. Append ("Username:"). Append (reader. GetString (0)). Append ("\ n")
. Append ("Password:"). Append (reader. GetString (1));
}
MessageBox.Show (sb.) ToString ());
Second, the use of dataset data set to the SQLite database to insert data, but also directly affixed to code: DialogResult dlgresult= openfiledialog1.showdialog (); Open the file you want to import
if (Openfiledialog1.filename = = "" | | Dlgresult! = DialogResult.OK)
Return
Reading text content using the StreamReader class
StreamReader sr=new StreamReader
(File.openread (Openfiledialog1.filename), System.Text.Encoding.Default);
Connecting to a database
System.Data.SQLite.SQLiteConnection conn = new System.Data.SQLite.SQLiteConnection ();
System.Data.SQLite.SQLiteConnectionStringBuilder ConnStr
= new System.Data.SQLite.SQLiteConnectionStringBuilder ();
ConnStr. DataSource = DataSource;
ConnStr. Password = "admin";//Set password, SQLite ADO. NET implements the database password protection
Conn. ConnectionString = ConnStr. ToString ();
Conn. Open ();
When a large number of updates are transacted, the transaction is cached, and then the bulk commit is sqlitedataadapter.update
Sqlitetransaction TS = conn. BeginTransaction ();
String sql= "Select Name,number from Test limit 1";
Sqlitedataadapter DTA = new Sqlitedataadapter (sql,conn);
Sqlitecommandbuilder SCB = new Sqlitecommandbuilder (DTA);
Dta. INSERTCOMMAND=SCB. GetInsertCommand ();
DataSet DS = new DataSet ();
Dta. FillSchema (Ds,schematype.source, "Temp"); Load Table schema Note
Dta. Fill (DS, "Temp"); Loading table Data
DataTable DT = DS. tables["Temp"];
Inserting data
while (!SR. Endofstream)
{
String[] Strarr = Sr. ReadLine (). Split (new char[] {' \ t '});
if (strarr[0]! = "" && strarr[1]! = "")
{
DataRow DR = DT. NewRow ();
DR[0]=STRARR[0];
DR[1]=STRARR[1];
Dt. Rows.Add (DR);
}
}
int RESULT=DTA. Update (DT); If you do not use BeginTransaction and commit bulk commit transactions, performance will be very low, 350 data more than 20 seconds
Ts.commit (); Commit a transaction
Ds. AcceptChanges ();
Freeing resources
Dta. Dispose ();
Ds. Clear ();
Conn. Close ();
Conn. Dispose ();
Sr. Close ();
Sr. Dispose ();
MessageBox.Show ("Successfully imported:" + result.) ToString () + "row data. "," hint ", MessageBoxButtons.OK, MessageBoxIcon.Information);