在C 專案中使用SQLite(環境安裝問題)

2021-10-19 22:52:17 字數 2404 閱讀 1081

下面這兩個是不同的:

1.system.data.sqlite (

2.sqlite(

儘管是乙個**,但是,是兩個產品。前者可用於c#,後者可以直接使用。前者的安裝目錄包含的檔案很多,後者包含很少。詳細差別可以自行查詢。

然後在專案中新增system.data.sqlite.dll:專案右鍵——新增——引用——瀏覽——安裝目錄——system.data.sqlite.dll——確定。如下圖:

新增完成後,在專案的引用目錄中會出現system.data.sqlite,如下圖:

這時候就可以新建專案使用sqlite了。需要using system.data.sqlite;此時在專案的bin/debug資料夾中會有system.data.sqlite.dll和system.data.sqlite.dll.config。

c#中資料庫訪問使用的是ado.net模型。使用sqlite也不例外。

(如果有問題,可以從system.data.sqlite的安裝目錄中複製system.data.sqlite.dll.config到專案的資料夾中,和上圖中的allforms、properties等在相同資料夾中)

(上圖中**使用的並不是sqlite資料庫,而是sql server資料庫)

在c#專案中建立資料庫:

var filename = "d:/testdb.db";

sqliteconnection.createfile(filename);

相應位置就出現了資料庫檔案。

這時候可以用sqlite的圖形介面管理工具驗證該資料庫。

成功建立資料庫。接下來在vs中c#專案裡連線該資料庫並建立乙個表。

連線資料庫:

string databasefilename = "d:/testdb.db";

string connectionstring = "data source = " + databasefilename;

sqliteconnection dbconnection = new sqliteconnection(connectionstring);

dbconnection.open();

此時出了些問題:

1.缺少sqlite.interop.dll。

只要從sqlite的安裝目錄中的bin目錄下找到該檔案,並複製到專案目錄bin的debug中即可。

2.試圖載入格式不正確的程式。

原來這是64位應用32位產生的問題。就是平台和軟體的版本不相容。我的電腦是64位的,我裝的system.data.sqlite也是64位的,但是專案屬性——生成——平台目標卻是any cpu(首選32位),將平台目標改為64位後,問題消失了。

建立乙個表:

string colnames = new string ;

string coltypes = new string ;

string tablename = "table1";

string querystring = "create table if not exists " + tablename + "( " + colnames[0] + " " + coltypes[0];

for (int i = 1; i < colnames.length; i++)

querystring += "  ) ";

sqlitecommand dbcommand = dbconnection.createcommand();

dbcommand.commandtext = querystring;

sqlitedatareader datareader = dbcommand.executereader();

成功連線資料庫並建立了乙個表,用圖形介面管理工具,會看到:

可以在c#專案中建立資料庫、連線資料庫、建立表,其他操作還沒有測試。

也可以通過vs選單欄中的專案——管理nuget程式包——瀏覽——system.data.sqlite——安裝

使用此方法時要注意專案的.net framework框架的版本,因為可能會出現新增了system.data.sqlite後在專案中卻不能使用sqlite的問題,using找不到此型別。

在c 中使用SQlite

1.生成 lib 檔案 第一步 找到lib.exe所在目錄 一般都在x program files microsoft visual studio vc98 bin下,在 執行 中輸入cmd,然後切換到該目錄下 第二步 使用lib命令生成.lib檔案 很多網頁上都介紹,使用lib def sqlit...

在專案中使用ExtJS

今天extjs官網發布了extjs最新正式版4.2.1。extjs為開發者在開發富客戶的b s應用中提供豐富的ui元件,具有統一的主題,便於快速開發,提高效率。但顯然它並不適合互聯 的開發。builds 壓縮後的extjs 體積更小,更快 docs 開發文件 examples 官方演示示例 loca...

在專案中使用springmvc

springmvc是spring框架的乙個模組,springmvc和spring無需通過中間整個層進行整合,它是乙個基於mvc的web框架。springmvc是基於方法開發的,struts2是基於類開發的。springmvc將url和controller方法對映,對映成功後springmvc生成乙個...