C # Use System. Data. SQLite to operate SQLite

Source: Internet
Author: User

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
  1. Static void Main (string [] args)
  2. {
  3. SQLiteConnection conn = null;
  4. String dbPath = "Data Source =" + Environment. CurrentDirectory + "/test. db ";
  5. Conn = new SQLiteConnection (dbPath); // create a database instance and specify the file location
  6. Conn. Open (); // Open the database. It is automatically created if the file does not exist.
  7. String SQL = "CREATE TABLE IF NOT EXISTS student (id integer, name varchar (20), sex varchar (2);"; // TABLE creation statement
  8. SQLiteCommand created createtable = new SQLiteCommand (SQL, conn );
  9. CmdCreateTable. ExecuteNonQuery (); // If the table does not exist, create a data table.
  10. SQLiteCommand cmdInsert = new SQLiteCommand (conn );
  11. CmdInsert. CommandText = "insert into student VALUES (1, 'red', 'male')"; // INSERT several pieces of data
  12. CmdInsert. ExecuteNonQuery ();
  13. CmdInsert. CommandText = "insert into student VALUES (2, 'lil', 'femal ')";
  14. CmdInsert. ExecuteNonQuery ();
  15. CmdInsert. CommandText = "insert into student VALUES (3, 'xiaoming ', 'male ')";
  16. CmdInsert. ExecuteNonQuery ();
  17. Conn. Close ();
  18. }

 

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
  1. Static void Main (string [] args)
  2. {
  3. String dbPath = Environment. CurrentDirectory + "/test. db"; // specify the database path
  4. Using (SQLiteConnection conn = new SQLiteConnection ("Data Source =" + dbPath) // create a connection
  5. {
  6. Conn. Open (); // Open the connection
  7. Using (SQLiteTransaction tran = conn. BeginTransaction () // instantiate a transaction
  8. {
  9. For (int I = 0; I <100000; I ++)
  10. {
  11. SQLiteCommand cmd = new SQLiteCommand (conn); // instantiate an SQL command
  12. Cmd. Transaction = tran;
  13. Cmd. CommandText = "insert into student values (@ id, @ name, @ sex)"; // sets an SQL statement with a parameter
  14. Cmd. Parameters. AddRange (new [] {// Add Parameters
  15. New SQLiteParameter ("@ id", I ),
  16. New SQLiteParameter ("@ name", "Chinese "),
  17. New SQLiteParameter ("@ sex", "male ")
  18. });
  19. Cmd. ExecuteNonQuery (); // execute the query
  20. }
  21. Tran. Commit (); // submit
  22. }
  23. }
  24. }

 

It takes about 5 seconds to insert such 100,000 data records.

 

Read data:

 

C # code
  1. Static void Main (string [] args)
  2. {
  3. SQLiteConnection conn = null;
  4. String dbPath = "Data Source =" + Environment. CurrentDirectory + "/test. db ";
  5. Conn = new SQLiteConnection (dbPath); // create a database instance and specify the file location
  6. Conn. Open (); // Open the database. It is automatically created if the file does not exist.
  7. String SQL = "select * from student ";
  8. SQLiteCommand cmdQ = new SQLiteCommand (SQL, conn );
  9. SQLiteDataReader reader = cmdQ. ExecuteReader ();
  10. While (reader. Read ())
  11. {
  12. Console. WriteLine (reader. GetInt32 (0) + "" + reader. GetString (1) + "" + reader. GetString (2 ));
  13. }
  14. Conn. Close ();
  15. Console. ReadKey ();
  16. }

 

 

Data is read successfully.

 

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.