SQLite Operations Summary

Source: Internet
Author: User
Tags sqlite

Reference:

System.Data.SQLite.dll

System.Data.SqlClient.dll

Note:: Sqlite.interop.076.dll Such DLLs must be placed on the WinCE device ("Computer \bt-w_series\\\windows" or "program root")

Using system;using system.data.sqlite;namespace framework.dataaccess{public class Sqlitemager {//database connection        Sqliteconnection m_dbconnection;        static void Main (string[] args) {Sqlitemager p = new Sqlitemager ();            } public Sqlitemager () {createnewdatabase ();            Connecttodatabase ();            CreateTable ();            Filltable ();        Printhighscores (); }//Create an empty database public void Createnewdatabase () {sqliteconnection.createfile ("mydatabase.sq        Lite "); }//Create a connection to the specified database public void Connecttodatabase () {m_dbconnection = new Sqliteconnection (" Data Source=mydatabase.sqlite;                   version=3; ");        M_dbconnection.open (); }//creates a table public void CreateTable () {String sql = "CREATE Table Highscores (NA) in the specified database            Me varchar (), score int) "; Sqlitecommand command = new Sqlitecommand (SQL, m_dbconnection); Command.        ExecuteNonQuery (); }//Insert some data public void filltable () {String sql = ' INSERT into Highscores (name, score) VA            Lues (' Me ', 3000) ";            Sqlitecommand command = new Sqlitecommand (sql, m_dbconnection); Command.            ExecuteNonQuery ();            sql = "INSERT into Highscores (name, score) VALUES (' myself ', 6000)";            Command = new Sqlitecommand (sql, m_dbconnection); Command.            ExecuteNonQuery ();            sql = "INSERT into Highscores (name, score) VALUES (' and I ', 9001)";            Command = new Sqlitecommand (sql, m_dbconnection); Command.        ExecuteNonQuery ();  }//Use SQL query statement and display result public void Printhighscores () {String sql = "SELECT * FROM Highscores            ORDER BY score Desc ";            Sqlitecommand command = new Sqlitecommand (sql, m_dbconnection); Sqlitedatareader reader = command.            ExecuteReader (); while (reader. READ ()) Console.WriteLine ("Name:" + reader["name"] + "\tscore:" + reader["score"]);        Console.ReadLine (); }    }}

  

Eg::

#region SQLite operation (check if database and table exist, create if not present)

If the database does not exist, create the database MyDatabase
if (! File.exists ("Mydatabase.sqlite")) Sqliteconnection.createfile ("Mydatabase.sqlite");

To create a connection to a database MyDatabase
Sqlitehelper _sqlitehelper = new Sqlitehelper ("Data source=mydatabase.sqlite; Version=3;datetimeformat=ticks; ");


Execute in MyDatabase database-CREATE TABLE
_sqlitehelper.executenonquery (CommandType.Text, "CREATE TABLE IF not EXISTS highscores (name varchar (), score int)");

DataSet obj2 = _sqlitehelper.executedataset (CommandType.Text, "SELECT * from Highscores ORDER BY score Desc");

int DT2 = Obj2. Tables[0]. Rows.Count;

Executing in mydatabase database-inserting data
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me ', 3000)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me2 ', 3002)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me ', 3000)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me2 ', 3002)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me ', 3000)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me2 ', 3002)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me ', 3000)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me2 ', 3002)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me ', 3000)");
_sqlitehelper.executenonquery (CommandType.Text, "insert into Highscores (name, score) VALUES (' Me2 ', 3002)");
In the MyDatabase database execution-Query
DataSet obj = _sqlitehelper.executedataset (CommandType.Text, "SELECT * from Highscores ORDER BY score Desc");

int dt= obj. Tables[0]. Rows.Count;
Sqlitemager Ddds = new Sqlitemager ();

#endregion

SQLite Operations Summary

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.