论坛首页 编程语言技术论坛

在.NET C#中使用sqlite

浏览 28750 次
精华帖 (0) :: 良好帖 (1) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-08-19  
sqlite是最近比较火的一个小型embeddable RDBMS。 用C实现的,开源,memory footprint非常小,而且对不同的语言有很多wrapper支持。

在.NET中使用sqlite,其实很简单,主要是找个.NET的wrapper。看了几个,最终选了phpguru的SQLite.NET

把sqlite.dll和SQLiteClient.dll放在.NET的current path下面,在项目的references中添加SQLiteClient.dll

程序中引用
c# 代码
 
  1. using SQLite.NET;  

使用方法,打开db:
c# 代码
 
  1. try  
  2.             {  
  3.                 Console.WriteLine("opening db...");  
  4.                 // Open database  
  5.                 SQLiteClient db = new SQLiteClient("c:\test.db");  
  6.   
  7.             }  
  8.             catch (SQLiteException e)  
  9.             {  
  10.                 Console.WriteLine("Fatal error: {0}", e.Message);  
  11.                 return;  
  12.             }  
select应用:
c# 代码
 
  1. ArrayList tables = db.GetColumn("SELECT name FROM sqlite_master WHERE type = 'table'");  
  2.   
  3.             foreach (string tableName in tables)  
  4.             {  
  5.                 Console.WriteLine("\t" + tableName);  
  6.             }  

其他update,insert,delete都支持得不错。 sqlite小巧玲珑,用起来十分方便。
Google Gears和Adobe AIR都在使用sqlite,看来必有其过人之处。。。
   发表时间:2007-08-25  
我用的是System.Data.Sqlite.

它只需要一个dll,接口基本符合ADO.Net 2.0的定义,性能也还不错,NHibernate用的也是它.
0 请登录后投票
   发表时间:2007-08-26  
多谢deerchao,我发现phpguru的SQLite.NET只支持sqlite2.
前日刚好找到System.Data.Sqlite,这今天试试,呵呵!
0 请登录后投票
   发表时间:2007-08-26  
System.Data.Sqlite入手。。。
首先import/using:
using System.Data.SQLite;


Connection和Command:
private SQLiteConnection conn;
        
        private SQLiteCommand cmd;


连接db:
conn = new SQLiteConnection("Data Source=c:\\test.db");
                conn.Open();


INSERT/UPDATE:

cmd = conn.CreateCommand();
cmd.CommandText = "INSERT INTO user(email,name) VALUES ('email','name')";
cmd.ExecuteNonQuery();

cmd.CommandText = "UPDATE userSET name = 'Codelicious' WHERE ID = 1";
cmd.ExecuteNonQuery();


SELECT:

cmd.CommandText = "SELECT ID, name FROM user";
SQLiteDataReader reader = cmd.ExecuteReader();
if (reader.HasRows)
            {
                while (reader.Read())
                {
                    Console.WriteLine("ID: " + reader.GetInt16(0));
                    Console.WriteLine("name: " + reader.GetString(1));
                }
            }


轻松搞定!
0 请登录后投票
   发表时间:2007-09-04  
System.Data.Sqlite支持wince啊,
早点看到就不用自己辛苦。
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics