一、关于SQLite : System.Data.SQLite 是一个原始 SQLite 的加强版。它不需要链接 .NET Runtime,因此可脱离 .NET 独立发布,它内嵌了一个完整的 2.0 引擎,为开发提供了完整的支持。
二、在C#中使用SQLite1. 添加引用: System.Data.SQLite.DLL 。
2. 创建数据库文件: SQLiteConnection.CreateFile(fileName)
;3. 连接数据库: var connection = new SQLiteConnection(connectionString);connectionString 中包含了数据库的一些配置信息,比如数据库文件路径,数据库密码等,可用 SQLiteConnectionStringBuilder 来创建 connectionString,当然也有其他方法,都很简单。
4. 从数据库读取数据。三、代码:view sourceprint?// 注意此处 using 。
using System;using System.Data.SQLite;using System.Text;namespace ConsoleApplication{public class SQLiteTester{public static void Test(){// 1.创建一个数据库文件。
var databaseFileName = "D:/test.db3";SQLiteConnection.CreateFile(databaseFileName)
;// 2.创建数据库连接。
var connectionString = new SQLiteConnectionStringBuilder{DataSource = databaseFileName,Password = "xxxxxx" // 此处假设数据库密码为: xxxxxx。};// 当然也可以直接: var connection = new SQLiteConnection("data source=" + databaseFileName + ";password=" + Password)
;using (var connection = new SQLiteConnection(connectionString.ToString())){// 3.打开连接。connection.Open();