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 a DB instance, specify the 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 the 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); Instantiating SQL commands
- Cmd. Transaction = Tran;
- Cmd.commandtext = "INSERT into student values (@id, @name, @sex)"; Setting 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 a DB instance, specify the 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.