SQLite,是一款輕型的數(shù)據(jù)庫,是遵守ACID的關(guān)聯(lián)式數(shù)據(jù)庫管理系統(tǒng),它的設(shè)計(jì)目標(biāo)是嵌入式的,而且目前已經(jīng)在很多嵌入式產(chǎn)品中使用了它,它占用資源非常的低,在嵌入式設(shè)備中,可能只需要幾百K的內(nèi)存就夠了。它能夠支持Windows/linux/Unix等等主流的操作系統(tǒng),同時(shí)能夠跟很多程序語言相結(jié)合,比如 Tcl、C#、php、java等,還有ODBC接口,同樣比起MySQL、PostgreSQL這兩款開源世界著名的數(shù)據(jù)庫管理系統(tǒng)來講,它的處理速度比他們都快。SQLite第一個(gè)Alpha版本誕生于2000年5月。 至今已經(jīng)有13個(gè)年頭,SQLite也迎來了一個(gè)版本 SQLite 3已經(jīng)發(fā)布。
連接Sqlite首先需要添加System.Data.SQLite.dll和System.Data.SQLite.Linq.dll的引用,這兩個(gè)dll文件你可以根據(jù)你的操作系統(tǒng)版本選擇合適的安裝版本,對于這兩個(gè)dll網(wǎng)上有的說放在system32或者指定目錄下,然后注冊,但是我按照那種做法就會出現(xiàn)“模塊 ***.dll 已加載,但找不到入口點(diǎn)DllRegisterSever”這個(gè)問題,我也嘗試著按網(wǎng)上的方法去解決,但是最后還是沒有解決,尼瑪,我果斷不管它,直接在項(xiàng)目里面引用這兩個(gè)dll文件。
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 using System.Data.SQLite ; 9 10 namespace Connect11 {12 public partial class Form1 : Form13 {14 public static SQLiteConnection sqlConn;15 public static string ConnectionString = "Data Source=E:/instance;Pooling=true;Integrated Security=True";//存儲公用的數(shù)據(jù)庫連接字符串16 17 18 //打開數(shù)據(jù)庫連接19 public static SQLiteConnection StartSQLConn()20 {21 sqlConn = new SQLiteConnection(ConnectionString);22 try23 {24 sqlConn.Open();25 }26 catch27 {28 sqlConn = null;29 }30 return sqlConn;31 }32 public static bool GetSQLCmd(string SQLstr)33 {34 bool flag = false;35 StartSQLConn();36 if (sqlConn != null)37 {38 SQLiteCommand SQLcmd = new SQLiteCommand(SQLstr, sqlConn);39 try40 {41 if (SQLcmd.ExecuteNonQuery() > 0)//執(zhí)行SQL語句42 flag = true;43 }44 catch45 { }46 sqlConn.Close();47 }48 return flag;49 }50 public Form1()51 {52 InitializeComponent();53 }54 55 PRivate void button1_Click(object sender, EventArgs e)56 {57 GetSQLCmd("insert into circuit(instance,user,coordinate,path,time)values('sun','qin','xin','hao','2014-04-23 14:55:10')");60 }61 }62 }
本以為一切都OK,誰知運(yùn)行后彈出錯(cuò)誤“混合模式程序集是針對“v2.0.50727”版的運(yùn)行時(shí)生成的,在沒有配置其他信息的情況下,無法在 4.0 運(yùn)行時(shí)中加載該程序集。”,對于這個(gè)問題,網(wǎng)上有說在app.config 中的configuration 節(jié)點(diǎn)中添加以下設(shè)置:
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
我覺得實(shí)在不愿再折騰,就直接關(guān)掉VS2010,用VS2008進(jìn)行編譯,誰知,一切OK。
以上是我首次使用C#連接SQLite,也是首次使用SQLite遇到的一些問題,不喜勿噴哦!
新聞熱點(diǎn)
疑難解答
圖片精選