Using System.Data.SQLite
:Http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
Get System.Data.SQLite.dll added to the project reference;
Build tables, insert operations
C # code
- Static void Main (string[] args)
- {
- Sqliteconnection conn = null;
- string dbPath = " Data Source =" + environment.currentdirectory + "/test.db";
- conn = new sqliteconnection (DbPath); //Create DB instance, specify file location
- Conn. Open (); //Open database, automatically created if file does not exist
- string sql = "CREATE TABLE IF not EXISTS student (ID integer, name varchar (), sex varchar (2));"; //Build Table statement
- Sqlitecommand cmdcreatetable = new sqlitecommand (SQL, conn);
- Cmdcreatetable.executenonquery (); //If the table does not exist, create a data table
- Sqlitecommand Cmdinsert = new sqlitecommand (conn);
- Cmdinsert.commandtext = "INSERT into student VALUES (1, ' Little Red ', ' Male ')"; //Insert several data
- Cmdinsert.executenonquery ();
- Cmdinsert.commandtext = "INSERT into student VALUES (2, ' Xiao Li ', ' female ')";
- Cmdinsert.executenonquery ();
- Cmdinsert.commandtext = "INSERT into student VALUES (3, ' xiaoming ', ' Male ')";
- Cmdinsert.executenonquery ();
- Conn. Close ();
- }
You can use SQLite Database browser to view data:
: http://sourceforge.net/projects/sqlitebrowser/
The table was built successfully.
Of course, this method of inserting data is not efficient, the amount of data is large, use the following method:
C # code
- Static void Main (string[] args)
- {
- string DbPath = environment.currentdirectory + "/test.db"; //Specify Database path
- using (Sqliteconnection conn = new sqliteconnection ("Data Source =" + dbPath) //Create connection
- {
- Conn. Open (); //Open Connection
- using (Sqlitetransaction tran = conn. BeginTransaction ())//instantiation of a transaction
- {
- for (int i = 0; i < 100000; i++)
- {
- Sqlitecommand cmd = new sqlitecommand (conn); //Instantiate SQL command
- Cmd. Transaction = Tran;
- Cmd.commandtext = "INSERT into student values (@id, @name, @sex)"; //Set the SQL statement with Parameters
- Cmd. Parameters.addrange (new[] {//Add parameter
- New Sqliteparameter ("@id", i),
- New Sqliteparameter ("@name", "Chinese"),
- New Sqliteparameter ("@sex", "male")
- });
- Cmd. ExecuteNonQuery (); //Execute Query
- }
- Tran.commit (); //Submit
- }
- }
- }
It only takes about 5 seconds to insert such a 100,000-piece data.
Read data:
C # code
- Static void Main (string[] args)
- {
- Sqliteconnection conn = null;
- string dbPath = " Data Source =" + environment.currentdirectory + "/test.db";
- conn = new sqliteconnection (DbPath); //Create DB instance, specify file location
- Conn. Open (); //Open database, automatically created if file does not exist
- string sql = "SELECT * from student";
- Sqlitecommand CMDQ = new sqlitecommand (SQL, conn);
- Sqlitedatareader reader = Cmdq.executereader ();
- while (Reader. Read ())
- {
- Console.WriteLine (reader. GetInt32 (0) + "" + reader. GetString (1) + "" + reader. GetString (2));
- }
- Conn. Close ();
- Console.readkey ();
- }
data read succeeded .