vs2010 net4.0 c# 操作 sqlite


1、百科介紹

SQLite,是一款輕型的數據庫,是遵守ACID的關系型數據庫管理系統,它包含在一個相對小的C庫中。它是D.RichardHipp建立的公有領域項目。它的設計目標是嵌入式的,而且目前已經在很多嵌入式產品中使用了它,它占用資源非常的低,在嵌入式設備中,可能只需要幾百K的內存就夠了。它能夠支持Windows/Linux/Unix等等主流的操作系統,同時能夠跟很多程序語言相結合,比如 Tcl、C#、PHP、Java等,還有ODBC接口,同樣比起Mysql、PostgreSQL這兩款開源的世界著名數據庫管理系統來講,它的處理速度比他們都快。SQLite第一個Alpha版本誕生於2000年5月。 至今已經有14個年頭,SQLite也迎來了一個版本 SQLite 3已經發布。

2、下載安裝

  1. http://www.sqlite.org/download.html  在Precompiled Binaries for Windows  下載一個shell版本,可以解壓、並將解壓后的目錄添加到系統的 PATH 變量中,這樣在cmd中可以直接使用,當然用的不多也可以每次都cd到目錄執行

 

2.http://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki 按照net版本選擇的下載

如果是vs2010 請下載 http://system.data.sqlite.org/downloads/1.0.94.0/sqlite-netFx40-setup-bundle-x86-2010-1.0.94.0.exe

3、基本SQl語句

1.建庫 sqlite3 test.db 

(問題:一般會出現near "sqlite3":syntax error ,但是搜索還找不到,有知道怎么回事的請告知一下;解決方法:

sqlite3 d:/test.db;)
2.建表create table testtable(id integer primary key, testname varchar(100));

  
3、插入數據

4、查詢數據

5.  .quit 退出, 其他命令請.help查看

4、開始c#操作sqlite
1、先去下載system.data.sqlite,安裝一下,建立一個Console程序把System.Data.SQLite.dll 和 System.Data.SQLite.Linq.dll拷貝出來引用
2、第一步創建庫和連接數據庫
 1  string FilePath =@"D:\test.db";
 2             if (!File.Exists(FilePath))
 3             {
 4                 System.Data.SQLite.SQLiteConnection.CreateFile(FilePath);
 5             }
 6             SQLiteConnection Conn = new SQLiteConnection();
 7             SQLiteConnectionStringBuilder ConnStr = new SQLiteConnectionStringBuilder();
 8             ConnStr.DataSource = FilePath;
 9             ConnStr.Password = "pguser";
10             ConnStr.Pooling = true;
11             Conn.ConnectionString = ConnStr.ToString();
12             Conn.Open();

3、創建表

  //創建表
            SQLiteCommand cmd = new SQLiteCommand();
            string sql = "CREATE TABLE  Xlog(logtype varchar(20),content varchar(400))";
            cmd.CommandText = sql;
            cmd.Connection = Conn;
            cmd.ExecuteNonQuery();
            Conn.Dispose();

  4、插入數據

 string sql1 = "insert into Xlog(logtype,content) VALUES ('test1' ,'test2')";
            SQLiteCommand cmd1 = new SQLiteCommand();
            cmd1.CommandText = sql1;
            cmd1.Connection = Conn;
            cmd1.ExecuteNonQuery();
            Conn.Dispose();

  5、查詢

string sql3 = "select * from Xlog";
            SQLiteCommand cmd2 = new SQLiteCommand();
            cmd2.Connection = Conn;
            cmd2.CommandText = sql3;         
            SQLiteDataReader reader =cmd2.ExecuteReader();
            StringBuilder sb = new StringBuilder();
            while (reader.Read())
            { sb.Append("logtype:"+reader.GetString(0)); }
            //Conn.Dispose();
            Conn.Close();
            Console.WriteLine(sb.ToString());
            Console.Read();

  基礎的操作已經完成,其他擴展就需要大家自己baidu和閱讀http://www.sqlite.org/docs.html

 源碼也留一下:http://files.cnblogs.com/skyapplezhao/sqliteConsoleTest1.rar 有需要的可以下載查看!




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM