Slqlite is a lightweight database that is currently a relatively popular small database for every system. NET nature is also supported by
1. Add 2 references System.data.sqlite.linq,system.data.sqlite
2. Bulk Insert the following "must be placed in a transaction to speed up." Much faster than the loop insert, 20W test data, less than 0.5S complete. 】
String datasource = @ "D:\tmp\test.db"; This method will error if the file does not exist. You can use the IO implementation to create one, and then use this. System.Data.SQLite.SQLiteConnection.CreateFile (DataSource); Connection 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 TABLE DateTime dt1 = DateTime.Now; System.Data.SQLite.SQLiteCommand cmd = new System.Data.SQLite.SQLiteCommand (); String sql = "CREATE TABLE Test (username varchar (20))"; Cmd.commandtext = SQL; Cmd. Connection = conn; Cmd. ExecuteNonQuery (); using (System.Data.Common.DbTransaction Dbtrans = conn. BeginTransaction (){sql = "INSERT into Test VALUES (@q1)"; DbParameter Field1 = cmd. CreateParameter (); Field1.parametername = "@q1"; Cmd. Parameters.Add (FIELD1); for (int i = 0; i < 200001; i++) {//Insert data Field1.value = I.tostrin g (); Cmd.commandtext = SQL; Cmd. ExecuteNonQuery (); } dbtrans.commit (); } DateTime DT2 = DateTime.Now; TimeSpan ts = dt2-dt1; Console.WriteLine (TS); 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)); }//Console.WriteLine (sb.) ToString ()); Console.readkey ();
SQLite for C #