C # using System.Data.SQLite to manipulate SQLite

Source: Internet
Author: User
Tags sqlite database

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
  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 DB instance, specify file location
  6. Conn. Open (); //Open database, automatically created if file does not exist
  7. string sql = "CREATE TABLE IF not EXISTS student (ID integer, name varchar (), sex varchar (2));"; //Build Table statement
  8. Sqlitecommand cmdcreatetable = 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, ' Little Red ', ' Male ')"; //Insert several data
  12. Cmdinsert.executenonquery ();
  13. Cmdinsert.commandtext = "INSERT into student VALUES (2, ' Xiao Li ', ' female ')";
  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/





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
  1. Static void Main (string[] args)
  2. {
  3. string DbPath = environment.currentdirectory + "/test.db"; //Specify Database path
  4. using (Sqliteconnection conn = new sqliteconnection ("Data Source =" + dbPath) //Create connection
  5. {
  6. Conn. Open (); //Open Connection
  7. using (Sqlitetransaction tran = conn. BeginTransaction ())//instantiation of a transaction
  8. {
  9. for (int i = 0; i < 100000; i++)
  10. {
  11. Sqlitecommand cmd = new sqlitecommand (conn); //Instantiate SQL command
  12. Cmd. Transaction = Tran;
  13. Cmd.commandtext = "INSERT into student values (@id, @name, @sex)"; //Set the SQL statement with Parameters
  14. Cmd. Parameters.addrange (new[] {//Add parameter
  15. New Sqliteparameter ("@id", i),
  16. New Sqliteparameter ("@name", "Chinese"),
  17. New Sqliteparameter ("@sex", "male")
  18. });
  19. Cmd. ExecuteNonQuery (); //Execute Query
  20. }
  21. Tran.commit (); //Submit
  22. }
  23. }
  24. }

It only takes about 5 seconds to insert such a 100,000-piece data.

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 DB instance, specify file location
  6. Conn. Open (); //Open database, automatically created if 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 read succeeded .

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.