Even doing network applications, in the case of disconnection, also need to be considered locally stored data. Before SQLite appear under a large amount of data, we have been using ACCESS, a small amount of data, the file is stored. ACCESS does not support atomic transactions, (which always happens) in case of power failure causes data is difficult to recover.
A: install
SQLITE, is a lightweight database, a relational database management system to comply with the ACID. I directly using http://sqlite.phxsoftware.com/ (An Open Source at The ADO.NET Provider for SQLite Database Engine). The download is complete is an EXE, after the installation root directory as follows:
There is a test tool under Bin, you can view the performance indicators of local SQLITE run.
II: New Database
After installation, open visual studio, a new data connection, the data source can be seen that a plurality SQLite.
New connection, as shown below. SQLITE database, after saving a file.
Three: Database Maintenance
SQLITE data may be maintained in terms of VS, as shown below:
SQL Query Analyzer can be used similar function in VS, as shown below:
Four: Mixed Mode
Installation, can be directly referenced in the project set, more
System.Data.SQLite
System.Data.SQLite.Linq
Two assemblies, due http://sqlite.phxsoftware.com/ the mixed mode System.Data.SQLite assembly, is generated for "v2.0.50727" runtime version, without additional information in the configuration , the assembly can not be loaded in the 4.0 runtime. Therefore, the following parameters need to be configured in App.config.
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup useLegacyV2RuntimeActivationPolicy="true"> <supportedRuntime version="v4.0"/> </startup> </configuration>
五:SQLiteHelper
最后,提供一个自己写的SQLiteHelper:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.SQLite; using System.Data; using System.Data.Common; namespace Com.Luminji.DataService.SQLHelpers { public class SQLiteHelper { /// <summary> /// ConnectionString样例:Data Source=Test.db3;Pooling=true;FailIfMissing=false /// </summary> public static string ConnectionString { get; set; } private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, string cmdText, params object[] p) { if (conn.State != ConnectionState.Open) conn.Open(); cmd.Parameters.Clear(); cmd.Connection = conn; cmd.CommandText = cmdText; cmd.CommandType = CommandType.Text; cmd.CommandTimeout = 30; if (p != null) { foreach (object parm in p) cmd.Parameters.AddWithValue(string.Empty, parm); } } public static DataSet ExecuteQuery(string cmdText, params object[] p) { using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand command = new SQLiteCommand()) { DataSet ds = new DataSet(); PrepareCommand(command, conn, cmdText, p); SQLiteDataAdapter da = new SQLiteDataAdapter(command); da.Fill(ds); return ds; } } } public static int ExecuteNonQuery(string cmdText, params object[] p) { using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, conn, cmdText, p); return command.ExecuteNonQuery(); } } } public static SQLiteDataReader ExecuteReader(string cmdText, params object[] p) { using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, conn, cmdText, p); return command.ExecuteReader(CommandBehavior.CloseConnection); } } } public static object ExecuteScalar(string cmdText, params object[] p) { using (SQLiteConnection conn = new SQLiteConnection(ConnectionString)) { using (SQLiteCommand command = new SQLiteCommand()) { PrepareCommand(command, conn, cmdText, p); return command.ExecuteScalar(); } } } } }
This article Source: http://www.cnblogs.com/luminji/
Reproduced in: https: //www.cnblogs.com/zhangchenliang/archive/2012/04/12/2443994.html