. Net SQLite Database driver and System.Data.SQLite.dll download the latest address:
Http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki
SQLite Management tools:
Http://www.cr173.com/soft/94247.html
App. Config file modification:
[HTML]View Plaincopyprint?
- <? XML version="1.0"?>
- <configuration>
- <appSettings>
- <!--failifmissing=false means that the database is automatically created when it does not exist
- <add key= "dbsqlite" value= "data source=| datadirectory| DB.DB3; Pooling=true; Failifmissing=false "/>
- </appSettings>
- </configuration>
Note: If the development environment is 4.0, and System.Data.Sqlite is the lower version, the error message "Mixed-mode assemblies are generated for the v2.0.50727 version of the runtime and cannot be configured with no additional information in 4.0 Load the assembly in the runtime, the workaround is to add:
<startup uselegacyv2runtimeactivationpolicy= "true" >
<supportedruntime version= "v4.0"/>
</startup>
Database read-write assistant SqliteHelper.cs
[CSharp]View Plaincopyprint?
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Text;
- Using System.Data;
- Using System.Data.Common;
- Using System.Data.SQLite;
- Public class Sqlitehelper
- {
- Public sqlitehelper ()
- {
- //
- //todo: Add constructor logic here
- //
- }
- private static sqliteconnection getconnection ()
- {
- string connstr = system.configuration.configurationmanager.appsettings["Dbsqlite"]. ToString ();
- Sqliteconnection conn = new Sqliteconnection (CONNSTR);
- Conn. Open ();
- return conn;
- }
- public static int executesql (string sql)
- {
- using (Sqliteconnection conn = getconnection ())
- {
- var cmd = new Sqlitecommand (SQL, conn);
- return cmd. ExecuteNonQuery ();
- }
- }
- public static int executescalar (string sql)
- {
- using (Sqliteconnection conn = getconnection ())
- {
- var cmd = new Sqlitecommand (SQL, conn);
- object o = cmd. ExecuteScalar ();
- return Int. Parse (O.tostring ());
- }
- }
- public static Sqlitedatareader ExecuteReader (String sql)
- {
- Sqliteconnection conn = getconnection ();
- var cmd = new Sqlitecommand (SQL, conn);
- Sqlitedatareader myreader = cmd. ExecuteReader (commandbehavior.closeconnection);
- return myreader;
- }
- public static DataSet execdataset (String sql)
- {
- using (Sqliteconnection conn = getconnection ())
- {
- var cmd = new Sqlitecommand (SQL, conn);
- Sqlitedataadapter da = new Sqlitedataadapter (CMD);
- DataSet ds = new DataSet ();
- Da. Fill (DS);
- return DS;
- }
- }
- }
Apply Form1.cs in a form
[CSharp]View Plaincopyprint?
- //Determines whether the table exists, does not exist, and generates
- int result = Sqlitehelper.executescalar ("Select COUNT (*) from sqlite_master where type= ' table ' and name= ' TB '");
- if (result = = 0)
- {
- //Create a table
- Sqlitehelper.executesql ("CREATE TABLE [TB] (ID integer PRIMARY KEY autoincrement, [name] varchar (), [CreateDate] da Tetime Default (DateTime (' Now ', ' localtime ')));
- }
- Insert a row of data
- result = Sqlitehelper.executesql ("INSERT INTO [TB] (name) VALUES (' Luck ')");
- if (Result > 0)
- {
- string msg = "";
- //Read Data
- Sqlitedatareader dr = Sqlitehelper.executereader ("SELECT * from [TB]");
- if (Dr. Read ())
- {
- msg + = Dr[0] + "," + dr[1] + "," + dr[2 ";
- }
- MessageBox.Show (msg);
- }
WinForm Read and Write SQLite database example (RPM)