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: 

 代碼:

 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 }  

 

C#中使用SQLite

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.