C#中使用SQLite

來源:互聯網
上載者:User

(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: 

代碼:

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.NET\bin 目錄的:System.Data.SQLite.dll和System.Data.SQLite.dll(可選)

(3) 建立資料庫

......

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.