C#中使用SQLite

(1) 从下面的网址下载了 SQLite 版本(sqlite-netFx40-setup-bundle-x64-2010-1.0.83.0):

http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki

安 .cs 文件中使用了 using:using System.Data.SQLite;

增加代码:SQLiteConnection.CreateFile(dataSource); 运行时报错如下(第一次使用C#,请各位帮忙分析一下出错的原因):

未处理 System.BadImageFormatException

Message=未能加载文件或程序集“System.Data.SQLite, Version=1.0.83.0, Culture=neutral,

PublicKeyToken=db937bc2d44ff139”或它的某一个依赖项。试图加载格式不正确的程序。

Source=UseSQLite

FileName=System.Data.SQLite, Version=1.0.83.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139

FusionLog==== 预绑定状态信息 ===

日志: 用户 = yonghang-PC\yonghang

日志: DisplayName = System.Data.SQLite, Version=1.0.83.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139

(Fully-

specified)

日志: Appbase = file:///E:/Source/PC/UseSQLite/bin/Debug/

日志: 初始 PrivatePath = NULL

调用程序集: UseSQLite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null。

===

日志: 此绑定从 default 加载上下文开始。

日志: 未找到应用程序配置文件。

日志: 使用主机配置文件:

日志: 使用 C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config 的计算机配置文件。

日志: 策略后引用: System.Data.SQLite, Version=1.0.83.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139

日志: 尝试下载新的 URL file:///E:/Source/PC/UseSQLite/bin/Debug/System.Data.SQLite.DLL。

错误: 未能完成程序集的安装(hr = 0x8007000b)。探测终止。

StackTrace:

在 UseSQLite.Form1.CreateSqliteDatabase()

在 UseSQLite.Form1..ctor() 位置 E:\Source\PC\UseSQLite\Form1.cs:行号 22

在 UseSQLite.Program.Main() 位置 E:\Source\PC\UseSQLite\Program.cs:行号 18

在 System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)

在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)

在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object

state, Boolean ignoreSyncCtx)

在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object

state)

在 System.Threading.ThreadHelper.ThreadStart()

InnerException:

代码:

 1 using System;  
 2 using System.Collections.Generic;  
 3 using System.ComponentModel;  
 4 using System.Data;  
 5 using System.Drawing;  
 6 using System.Linq;  
 7 using System.Text;  
 8 using System.Windows.Forms;  
 9    
10 using System.Data.SQLite;  
11    
12 namespace UseSQLite  
13 {  
14     public partial class Form1 : Form  
15     {  
16         private string dataSource = "UseSQLite.sqlite";  
17         //private string dataSource = "E:/Source/PC/SQliteDB.db";  
18    
19         public Form1()  
20         {  
21             InitializeComponent();  
22    
23             CreateSqliteDatabase();  
24         }  
25    
26         void CreateSqliteDatabase()  
27         {  
28             System.Diagnostics.Debug.WriteLine("Use SQLite: start create database...");  
29             SQLiteConnection.CreateFile(dataSource);  
30 //             SQLiteConnection conn = new SQLiteConnection();  
31 //             SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();  
32 //             connstr.DataSource = dataSource;  
33 //             connstr.Password = "admin"; //设置密码,SQLite ADO.NET实现了数据库密码保护  
34 //             conn.ConnectionString = connstr.ToString();  
35 //             conn.Open();  
36             System.Diagnostics.Debug.WriteLine("Use SQLite: create database end.");  
37         }  
38     }  
39 }  

CSDN友回复可能是因为以下原因:

有可能是框架配置不正确,SQLite是.NET 4.0,而你的控制台为.NET 4.0 Client Profile;

.NET 4.0 大于 .NET 4.0 Client Profile 查看项目属性,更改试试.

.NET 4.0 Client Profile比.NET 4.0占用的空间要小,但支持的类型也比.NET 4.0支持的类型小。

SQLITE的动态库是.NET 4.0 Client Profile不支持的。

未验证是否是这样。

(2) 下载了:SQLite-1.0.66.0-setup.exe,安装后在 VS2010 的菜单“增加新数据源”的“新建连接...”中终于找到了 SQLite 的选项。

然后需要将工程的目标框架由 4.0 修改为 3.5,否则编译报错。

最后,在工程中“增加引用”,选择 \SQLite.NET\bin 目录的:System.Data.SQLite.dll和System.Data.SQLite.dll(可选)

(3) 创建数据库

......

------------------------------------------------------------------------------------------------

* 从 http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 下载了 sqlite-netFx35-setup-bundle-x86-2008-1.0.83.0.exe

* 为 VS2008 版本,安装。安装过程中,在 console 界面提示了一些错误,最后安装完成。

* 在 VS2008 中新建一个 C# 的工程,"Add References..." 浏览到 sqlite-netFx35-setup-bundle-x86-2008-1.0.83.0.exe 的安装目录

* 例如,我的安装目录:D:\Program Files\System.Data.SQLite\2008\bin

* 选择 System.Data.SQLite.dll,在工程的 References 中可以看到多了一个: System.Data.SQLite

* 在工程的 Form1.cs 中增加: using System.Data.SQLite;

* 增加代码如下:

* 编译调试通过,引起问题的原因如 CSDNer 所说的 .Net 版本选择不对,导致前天测试一直通不过。

 1 using System.Data.SQLite;  
 2   
 3 namespace TestUseSqlite  
 4 {  
 5     public partial class Form1 : Form  
 6     {  
 7         private string dataSource = "ContactBookDB.sqlite";  
 8   
 9         public Form1()  
10         {  
11             InitializeComponent();  
12   
13             SQLiteConnection.CreateFile(dataSource);  
14   
15             SQLiteConnection dbConn = new SQLiteConnection("Data Source=" + dataSource);  
16   
17             dbConn.Open();  
18   
19             SQLiteCommand dbCmd = dbConn.CreateCommand();  
20   
21             dbCmd.CommandText = "CREATE TABLE TelephoneBook(personID varchar(20),telephone varchar(30),type varchar(20))";  
22             dbCmd.ExecuteNonQuery();  
23   
24             dbCmd.CommandText = "INSERT INTO TelephoneBook VALUES('MTB','1234567890','not mobile')";  
25             dbCmd.ExecuteNonQuery();  
26   
27             dbCmd.CommandText = "SELECT * FROM TelephoneBook";  
28             SQLiteDataReader dataReader = dbCmd.ExecuteReader();  
29   
30             DataTable dataTable = new DataTable();  
31             if (dataReader.HasRows)  
32             {  
33                 dataTable.Load(dataReader);  
34             }  
35   
36             dataGridView1.DataSource = dataTable;  
37   
38             dataReader.Close();  
39             dbConn.Close();  
40         }  
41     }  
42 }