Use System. Data. SQLite
: Http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
Add System. Data. SQLite. dll to the project reference;
Create and insert tables
C # code
- Static void Main (string [] args)
- {
- SQLiteConnection conn = null;
- String dbPath = "Data Source =" + Environment. CurrentDirectory + "/test. db ";
- Conn = new SQLiteConnection (dbPath); // create a database instance and specify the file location
- Conn. Open (); // Open the database. It is automatically created if the file does not exist.
- String SQL = "CREATE TABLE IF NOT EXISTS student (id integer, name varchar (20), sex varchar (2);"; // TABLE creation statement
- SQLiteCommand created createtable = 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, 'red', 'male')"; // INSERT several pieces of data
- CmdInsert. ExecuteNonQuery ();
- CmdInsert. CommandText = "insert into student VALUES (2, 'lil', 'femal ')";
- 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/
Table created successfully.
Of course, this method is not efficient for data insertion. If the data volume 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 a connection
- {
- Conn. Open (); // Open the connection
- Using (SQLiteTransaction tran = conn. BeginTransaction () // instantiate a transaction
- {
- For (int I = 0; I <100000; I ++)
- {
- SQLiteCommand cmd = new SQLiteCommand (conn); // instantiate an SQL command
- Cmd. Transaction = tran;
- Cmd. CommandText = "insert into student values (@ id, @ name, @ sex)"; // sets an SQL statement with a parameter
- Cmd. Parameters. AddRange (new [] {// Add Parameters
- New SQLiteParameter ("@ id", I ),
- New SQLiteParameter ("@ name", "Chinese "),
- New SQLiteParameter ("@ sex", "male ")
- });
- Cmd. ExecuteNonQuery (); // execute the query
- }
- Tran. Commit (); // submit
- }
- }
- }
It takes about 5 seconds to insert such 100,000 data records.
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 database instance and specify the file location
- Conn. Open (); // Open the database. It is automatically created if the 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 is read successfully.