当前位置:首页 > 芯闻号 > 充电吧
[导读](1) 从下面的网址下载了 SQLite 版本(sqlite-netFx40-setup-bundle-x64-2010-1.0.83.0):http://system.data.sqlite.org

(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-PCyonghang
日志: 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:WindowsMicrosoft.NETFrameworkv4.0.30319configmachine.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:SourcePCUseSQLiteForm1.cs:行号 22
       在 UseSQLite.Program.Main() 位置 E:SourcePCUseSQLiteProgram.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: 


代码:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
using System.Data.SQLite;
 
namespace UseSQLite
{
    public partial class Form1 : Form
    {
        private string dataSource = "UseSQLite.sqlite";
        //private string dataSource = "E:/Source/PC/SQliteDB.db";
 
        public Form1()
        {
            InitializeComponent();
 
            CreateSqliteDatabase();
        }
 
        void CreateSqliteDatabase()
        {
            System.Diagnostics.Debug.WriteLine("Use SQLite: start create database...");
            SQLiteConnection.CreateFile(dataSource);
//             SQLiteConnection conn = new SQLiteConnection();
//             SQLiteConnectionStringBuilder connstr = new SQLiteConnectionStringBuilder();
//             connstr.DataSource = dataSource;
//             connstr.Password = "admin"; //设置密码,SQLite ADO.NET实现了数据库密码保护
//             conn.ConnectionString = connstr.ToString();
//             conn.Open();
            System.Diagnostics.Debug.WriteLine("Use SQLite: create database end.");
        }
    }
}




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.NETbin 目录的:System.Data.SQLite.dll和System.Data.SQLite.dll(可选)


(3) 创建数据库

......

本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭