C # using SQLite database

Source: Internet
Author: User
Tags mdb database sqlite database



This is just a case of not installing, only referencing DLLs in your project.
Download the zip package with the static word from the SQLite website, for example, I downloaded sqlite-netfx40-static-binary-win32-2010-1.0.94.0.zip.
After decompression can get a lot of files, which also contains the installation file Install.exe, but can not install, if you want to use SQLite, minimum two dll:system.data.sqlite.dll,sqlite.interop.dll required. If you need to use LINQ or entityframework, you'll need System.Data.SQLite.Linq.dll and System.Data.SQLite.EF6.dll.

Without considering Linq and entityframework, you only need to refer to System.Data.SQLite.dll, and then make sure that you can find SQLite.Interop.dll in the root directory of the program.

Place a SQLite database file at the root of the program (the database file path can actually be arbitrary). The sqliteconnection,sqlitecommand,sqlitedataadapter is provided in the System.Data.SQLite.


private void InitSqlite ()
{
     SQLiteConnection conn = new SQLiteConnection ("Data Source = test.db3");
     conn.Open ();
     var table = conn.GetSchema ("tables");
     // Get the table structure in the database file
     TableDataGrid.ItemsSource = table.DefaultView;

     var cmd = new SQLiteCommand (@ "select * from Demo", conn);
     SQLiteDataAdapter adapter = new SQLiteDataAdapter (cmd);
     DataSet ds = new DataSet ();
     adapter.Fill (ds);
     // Get the column structure of the table, get it by querying all
     ColumnDataGrid.ItemsSource = ds.Tables [0] .DefaultView;
     cmd.Dispose ();
     conn.Close ();
}




Similarly, executing the SQL statement that creates the database is also possible
private void CreateTable()
{
    SQLiteConnection conn = new SQLiteConnection ("Data Source=test.db3");
    conn.Open();
    var sqlStr = @"create table Demo([Id] int IDENTITY (1,1) PRIMARY KEY,[Pinyin] varchar(50) null)" ;
    SQLiteCommand cmd= new SQLiteCommand (sqlStr,conn);
    cmd.ExecuteNonQuery();
    cmd.Dispose();
    conn.Close();
}




SQLite database compression and MDB database, the SQLite database does not reclaim the control by default, after a large amount of data deletion, the volume of the database file will not change, to compress the database file, you can perform a VACUUM command. VACUUM will reorganize the database from the beginning. This will use the database with an empty "free list", and the database file will be minimal.
private void Compact()
{
    SQLiteConnection conn = new SQLiteConnection ( "Data Source=test.db3");
    conn.Open();
    SQLiteCommand cmd = new SQLiteCommand( "VACUUM" , conn);
    cmd.ExecuteNonQuery();
    cmd.Dispose();
    conn.Close();
}








C # using SQLite database


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.