C # Operations DataSet DataSet vs. SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database password protection

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);
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.