在VS2008中如何使用SQLite(C#版)

友情提示:本文所述代码下载地址 http://download.csdn.net/source/2299805

 

SQLite 是一个嵌入式的关系数据库系统,运用十分广泛。在一些数据处理量不大的应用程序中,使用SQLite可以很大程度的降低部署时的工作量。要使VS2008支持SQLite十分简单,只需要下载 SQLite-1.0.66.0-setup.exe,安装即可。下载地址http://download.csdn.net/detail/andylaufzf/3787179

打开Visual Studio 2008,新建一个Console Application,项目名称为SQLite,并为项目添加System.Data.SQLite的引用。再添加一个数据库连接,此时可以看到,更改数据源窗口中多了一个SQLite Database File的数据源选项,如图:

数据源 

挑选此类型的数据源,并且New一个Database文件test.db,密码123,如图:

新建数据库连接 

接下来再新数据库中添加一张表Book,如图:新建数据库表 

下面开始为此表建立一个Data Access类,以展示在VS2008中如何使用SQLite,可以想象,和操作其他数据库是几乎一样的。
首先,新建一个实体类 Book.cs 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace SQLite  
  7. {  
  8.     class Book  
  9.     {  
  10.         private int id;  
  11.         private string bookName;  
  12.         private decimal price;  
  13.   
  14.         public int ID  
  15.         {  
  16.             get { return id; }  
  17.             set { id = value; }  
  18.         }  
  19.   
  20.         public string BookName  
  21.         {  
  22.             get { return bookName; }  
  23.             set { bookName = value; }  
  24.         }  
  25.   
  26.         public decimal Price  
  27.         {  
  28.             get { return price; }  
  29.             set { price = value; }  
  30.         }  
  31.     }  
  32. }  

其次,编写DAL类:BookDAL  

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. using System.Data.SQLite;  
  7.   
  8. namespace SQLite  
  9. {  
  10.     class BookDAL  
  11.     {  
  12.         private const string sConn = "Data Source=G://Exercise//Visual Studio 2008//SQLite//test.db;Version=3;Password=123;";  
  13.   
  14.         public static bool Create(Book book)  
  15.         {  
  16.             try  
  17.             {  
  18.                 using (SQLiteConnection conn = new SQLiteConnection(sConn))  
  19.                 {  
  20.                       
  21.                     conn.Open();  
  22.                     SQLiteCommand cmd = conn.CreateCommand();  
  23.                     cmd.CommandText = "INSERT INTO book(ID,BookName,Price) VALUES(@ID,@BookName,@Price);";  
  24.                     cmd.Parameters.Add(new SQLiteParameter("ID", book.ID));  
  25.                     cmd.Parameters.Add(new SQLiteParameter("BookName", book.BookName));  
  26.                     cmd.Parameters.Add(new SQLiteParameter("Price", book.Price));  
  27.                     int i = cmd.ExecuteNonQuery();  
  28.                     return i == 1;  
  29.                 }  
  30.             }  
  31.             catch (Exception)  
  32.             {  
  33.                 //Do any logging operation here if necessary  
  34.                 return false;  
  35.             }  
  36.         }  
  37.   
  38.         public static bool Update(Book book)  
  39.         {  
  40.             try  
  41.             {  
  42.                 using (SQLiteConnection conn = new SQLiteConnection(sConn))  
  43.                 {  
  44.                     conn.Open();  
  45.                     SQLiteCommand cmd = conn.CreateCommand();  
  46.                     cmd.CommandText = "UPDATE Book SET BookName=@BookName,Price=@Price where ID=@ID;";  
  47.                     cmd.Parameters.Add(new SQLiteParameter("ID", book.ID));  
  48.                     cmd.Parameters.Add(new SQLiteParameter("BookName", book.BookName));  
  49.                     cmd.Parameters.Add(new SQLiteParameter("Price", book.Price));  
  50.                     int i = cmd.ExecuteNonQuery();  
  51.                     return i == 1;  
  52.                 }  
  53.             }  
  54.             catch (Exception)  
  55.             {  
  56.                 //Do any logging operation here if necessary  
  57.                 return false;  
  58.             }  
  59.         }  
  60.   
  61.         public static bool Delete(int ID)  
  62.         {  
  63.             try  
  64.             {  
  65.                 using (SQLiteConnection conn = new SQLiteConnection(sConn))  
  66.                 {  
  67.                     conn.Open();  
  68.                     SQLiteCommand cmd = conn.CreateCommand();  
  69.                     cmd.CommandText = "DELETE FROM Book WHERE ID=@ID";  
  70.                     cmd.Parameters.Add(new SQLiteParameter("ID", ID));  
  71.                     int i = cmd.ExecuteNonQuery();  
  72.                     return i == 1;  
  73.                 }  
  74.             }  
  75.             catch (Exception)  
  76.             {  
  77.                 //Do any logging operation here if necessary  
  78.                 return false;  
  79.             }  
  80.         }  
  81.   
  82.         public static Book GetbyID(int ID)  
  83.         {  
  84.             try  
  85.             {  
  86.                 using (SQLiteConnection conn = new SQLiteConnection(sConn))  
  87.                 {  
  88.                     conn.Open();  
  89.                     SQLiteCommand cmd = conn.CreateCommand();  
  90.                     cmd.CommandText = "SELECT * FROM Book WHERE ID=@ID;";  
  91.                     cmd.Parameters.Add(new SQLiteParameter("ID", ID));  
  92.                     SQLiteDataReader dr = cmd.ExecuteReader();  
  93.                     if (dr.Read())  
  94.                     {  
  95.                         Book book = new Book();  
  96.                         book.ID = dr.GetInt32(0);  
  97.                         book.BookName = dr.GetString(1);  
  98.                         book.Price = dr.GetDecimal(2);  
  99.                         return book;  
  100.                     }  
  101.                     else  
  102.                         return null;  
  103.                 }  
  104.             }  
  105.             catch (Exception)  
  106.             {  
  107.                 //Do any logging operation here if necessary  
  108.                 return null;  
  109.             }  
  110.         }  
  111.     }  
  112. }  

最后,编写测试主程序: 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace SQLite  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.             if (BookDAL.Delete(1) == false)  
  13.                 Console.WriteLine("删除 ID=1 失败");  
  14.   
  15.             if (BookDAL.Delete(2) == false)  
  16.                 Console.WriteLine("删除 ID=2 失败");  
  17.   
  18.             Book book = new Book();  
  19.   
  20.             book.ID = 1;  
  21.             book.BookName = "Book A";  
  22.             book.Price = 40.0m;  
  23.             BookDAL.Create(book);  
  24.   
  25.             book.ID = 2;  
  26.             book.BookName = "第二本书";  
  27.             book.Price = 42.0m;  
  28.             BookDAL.Create(book);  
  29.   
  30.             book = BookDAL.GetbyID(2);  
  31.             Console.WriteLine(book.ID + " " + book.BookName + " " + book.Price);  
  32.   
  33.             book.Price = 24.5m;  
  34.             BookDAL.Update(book);  
  35.   
  36.             book = BookDAL.GetbyID(2);  
  37.             Console.WriteLine(book.ID + " " + book.BookName + " " + book.Price);  
  38.             book = BookDAL.GetbyID(1);  
  39.             Console.WriteLine(book.ID + " " + book.BookName + " " + book.Price);   
  40.         }  
  41.     }  
  42. }  


执行结果:

2 第二本书 42
2 第二本书 24.5
1 Book A 40

                  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值