SQLite for C #

Source: Internet
Author: User
Tags bulk insert password protection

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 #

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.