最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#操作SQLite方法實(shí)例詳解

 更新時(shí)間:2015年06月05日 10:30:52   作者:方倍工作室  
這篇文章主要介紹了C#操作SQLite方法,以實(shí)例形式詳細(xì)分析了C#操作SQLite的連接、查詢、插入、修改等相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了C#操作SQLite方法。分享給大家供大家參考。具體分析如下:

地址:

System.Data.Sqlite入手。。。

首先import/using: 

復(fù)制代碼 代碼如下:
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)); 
  } 
}

模板程序:

using System; 
using System.Data; 
using System.Data.Common; 
using System.Data.SQLite; 
namespace SQLiteQueryBrowser 
{ 
   /// <summary> 
   /// 說明:這是一個(gè)針對(duì)System.Data.SQLite的數(shù)據(jù)庫常規(guī)操作封裝的通用類。 
   /// </summary> 
   public class SQLiteDBHelper 
   { 
     private string connectionString = string.Empty; 
     /// <summary> 
     /// 構(gòu)造函數(shù) 
     /// </summary> 
     /// <param name="dbPath">SQLite數(shù)據(jù)庫文件路徑</param> 
     public SQLiteDBHelper(string dbPath) 
     { 
       this.connectionString = "Data Source=" + dbPath; 
     } 
     /// <summary> 
     /// 創(chuàng)建SQLite數(shù)據(jù)庫文件 
     /// </summary> 
     /// <param name="dbPath">要?jiǎng)?chuàng)建的SQLite數(shù)據(jù)庫文件路徑</param> 
     public static void CreateDB(string dbPath) 
     { 
       using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbPath)) 
       { 
         connection.Open(); 
         using (SQLiteCommand command = new SQLiteCommand(connection)) 
         { 
           command.CommandText = "CREATE TABLE Demo(id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE)"; 
           command.ExecuteNonQuery(); 
           command.CommandText = "DROP TABLE Demo"; 
           command.ExecuteNonQuery(); 
         } 
       } 
     } 
     /// <summary> 
     /// 對(duì)SQLite數(shù)據(jù)庫執(zhí)行增刪改操作,返回受影響的行數(shù)。 
     /// </summary> 
     /// <param name="sql">要執(zhí)行的增刪改的SQL語句</param> 
     /// <param name="parameters">執(zhí)行增刪改語句所需要的參數(shù),參數(shù)必須以它們?cè)赟QL語句中的順序?yàn)闇?zhǔn)</param> 
     /// <returns></returns> 
     public int ExecuteNonQuery(string sql, SQLiteParameter[] parameters) 
     { 
       int affectedRows = 0; 
       using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 
       { 
         connection.Open(); 
         using (DbTransaction transaction = connection.BeginTransaction()) 
         { 
           using (SQLiteCommand command = new SQLiteCommand(connection)) 
           { 
             command.CommandText = sql; 
             if (parameters != null) 
             { 
               command.Parameters.AddRange(parameters); 
             } 
             affectedRows = command.ExecuteNonQuery(); 
           } 
           transaction.Commit(); 
         } 
       } 
       return affectedRows; 
     } 
     /// <summary> 
     /// 執(zhí)行一個(gè)查詢語句,返回一個(gè)關(guān)聯(lián)的SQLiteDataReader實(shí)例 
     /// </summary> 
     /// <param name="sql">要執(zhí)行的查詢語句</param> 
     /// <param name="parameters">執(zhí)行SQL查詢語句所需要的參數(shù),參數(shù)必須以它們?cè)赟QL語句中的順序?yàn)闇?zhǔn)</param> 
     /// <returns></returns> 
     public SQLiteDataReader ExecuteReader(string sql, SQLiteParameter[] parameters) 
     { 
       SQLiteConnection connection = new SQLiteConnection(connectionString); 
       SQLiteCommand command = new SQLiteCommand(sql, connection); 
       if (parameters != null) 
       { 
         command.Parameters.AddRange(parameters); 
       } 
       connection.Open(); 
       return command.ExecuteReader(CommandBehavior.CloseConnection); 
     } 
     /// <summary> 
     /// 執(zhí)行一個(gè)查詢語句,返回一個(gè)包含查詢結(jié)果的DataTable 
     /// </summary> 
     /// <param name="sql">要執(zhí)行的查詢語句</param> 
     /// <param name="parameters">執(zhí)行SQL查詢語句所需要的參數(shù),參數(shù)必須以它們?cè)赟QL語句中的順序?yàn)闇?zhǔn)</param> 
     /// <returns></returns> 
     public DataTable ExecuteDataTable(string sql, SQLiteParameter[] parameters) 
     { 
       using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 
       { 
         using (SQLiteCommand command = new SQLiteCommand(sql, connection)) 
         { 
           if (parameters != null) 
           { 
             command.Parameters.AddRange(parameters); 
           } 
           SQLiteDataAdapter adapter = new SQLiteDataAdapter(command); 
           DataTable data = new DataTable(); 
           adapter.Fill(data); 
           return data; 
         } 
       } 
     } 
     /// <summary> 
     /// 執(zhí)行一個(gè)查詢語句,返回查詢結(jié)果的第一行第一列 
     /// </summary> 
     /// <param name="sql">要執(zhí)行的查詢語句</param> 
     /// <param name="parameters">執(zhí)行SQL查詢語句所需要的參數(shù),參數(shù)必須以它們?cè)赟QL語句中的順序?yàn)闇?zhǔn)</param> 
     /// <returns></returns> 
     public Object ExecuteScalar(string sql, SQLiteParameter[] parameters) 
     { 
       using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 
       { 
         using (SQLiteCommand command = new SQLiteCommand(sql, connection)) 
         { 
           if (parameters != null) 
           { 
             command.Parameters.AddRange(parameters); 
           } 
           SQLiteDataAdapter adapter = new SQLiteDataAdapter(command); 
           DataTable data = new DataTable(); 
           adapter.Fill(data); 
           return data; 
         } 
       } 
     } 
     /// <summary> 
     /// 查詢數(shù)據(jù)庫中的所有數(shù)據(jù)類型信息 
     /// </summary> 
     /// <returns></returns> 
     public DataTable GetSchema() 
     { 
       using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 
       { 
         connection.Open(); 
         DataTable data=connection.GetSchema("TABLES"); 
         connection.Close(); 
         //foreach (DataColumn column in data.Columns) 
         //{ 
         //  Console.WriteLine(column.ColumnName); 
         //} 
         return data; 
       } 
     } 
   } 
}

完整的程序例子:

using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data; 
using System.Data.Common; 
using System.Data.SQLite; 
using SQLiteQueryBrowser; 
namespace SQLiteDemo 
{ 
   class Program 
   { 
     static void Main(string[] args) 
     { 
       //CreateTable(); 
       //InsertData(); 
       ShowData(); 
       Console.ReadLine(); 
     } 
     public static void CreateTable() 
     { 
       string dbPath = "D:\\Demo.db3"; 
       //如果不存在改數(shù)據(jù)庫文件,則創(chuàng)建該數(shù)據(jù)庫文件 
       if (!System.IO.File.Exists(dbPath)) 
       { 
         SQLiteDBHelper.CreateDB("D:\\Demo.db3"); 
       } 
       SQLiteDBHelper db = new SQLiteDBHelper("D:\\Demo.db3"); 
       string sql = "CREATE TABLE Test3(id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,Name char(3),TypeName varchar(50),addDate datetime,UpdateTime Date,Time time,Comments blob)"; 
       db.ExecuteNonQuery(sql, null); 
     } 
     public static void InsertData() 
     { 
       string sql = "INSERT INTO Test3(Name,TypeName,addDate,UpdateTime,Time,Comments)values(@Name,@TypeName,@addDate,@UpdateTime,@Time,@Comments)"; 
       SQLiteDBHelper db = new SQLiteDBHelper("D:\\Demo.db3"); 
       for (char c = "A"; c <= "Z"; c++) 
       { 
         for (int i = 0; i < 100; i++) 
         { 
           SQLiteParameter[] parameters = new SQLiteParameter[]{ 
             new SQLiteParameter("@Name",c+i.ToString()), 
           new SQLiteParameter("@TypeName",c.ToString()), 
           new SQLiteParameter("@addDate",DateTime.Now), 
           new SQLiteParameter("@UpdateTime",DateTime.Now.Date), 
           new SQLiteParameter("@Time",DateTime.Now.ToShortTimeString()), 
           new SQLiteParameter("@Comments","Just a Test"+i) 
           }; 
           db.ExecuteNonQuery(sql, parameters); 
         } 
       } 
     } 
     public static void ShowData() 
     { 
       //查詢從50條起的20條記錄 
       string sql = "select * from test3 order by id desc limit 50 offset 20"; 
       SQLiteDBHelper db = new SQLiteDBHelper("D:\\Demo.db3"); 
       using (SQLiteDataReader reader = db.ExecuteReader(sql, null)) 
       { 
         while (reader.Read()) 
         { 
           Console.WriteLine("ID:{0},TypeName{1}", reader.GetInt64(0), reader.GetString(1)); 
         } 
       } 
     } 
   } 
 }

在實(shí)際情況中,采用通用類大批量插入數(shù)據(jù)會(huì)有些慢,這是因?yàn)樵赟ystem.Data.SQLite中的操作如果沒有指定操作,則會(huì)被當(dāng)做一個(gè)事物,如果需要一次性寫入大量記錄,則建議顯式創(chuàng)建一個(gè)事物,在這個(gè)事務(wù)中完成所有的操作比較好,這樣的話比每次操作創(chuàng)建一個(gè)事物的效率要提升很多。

最終利用VS2008提供的功能,可以看到里面的數(shù)據(jù)如下:

需要說明的是在System.Data.SQLite中數(shù)據(jù)類型的規(guī)定不適很嚴(yán)格,從創(chuàng)建Test3表的SQL語句來看,表中addDate、UpdateTime、Time分別是DateTime、Date、Time類型字段,但實(shí)際上我們插入的時(shí)候沒有按照這個(gè)規(guī)定,最終顯示的結(jié)果也是盡量遵循數(shù)據(jù)庫字段的定義。

總結(jié)

System.Data.SQLite確實(shí)是一個(gè)非常小巧精悍的數(shù)據(jù)庫,作為對(duì)SQLite的封裝(SQLite可以在Android等類型的手機(jī)上利用Java訪問),它依然是體較小,同比性能高、內(nèi)存消耗小、無需安裝僅需一個(gè)dll就可以運(yùn)行的優(yōu)點(diǎn)(如果在Mobile手機(jī)上則需要兩個(gè)文件),唯一的一個(gè)缺點(diǎn)是沒有比較的GUI(圖形用戶界面),不過正因?yàn)槿绱怂诺靡泽w積小。

在實(shí)際開發(fā)中沒有圖形用戶界面可能有些不便,我們可以使用VS來查看和操作數(shù)據(jù),我自己也做了一個(gè)小東東,便于管理和維護(hù)數(shù)據(jù),界面如下:

如果你要開發(fā)數(shù)據(jù)量在10萬條以下的應(yīng)用,我建議你嘗試使用一下System.Data.SQLite,它或許是一個(gè)不錯(cuò)的選擇。

public static void CreateTable()
{
 string dbPath = "D:\\Demo.db3";
 //如果不存在改數(shù)據(jù)庫文件,則創(chuàng)建該數(shù)據(jù)庫文件 
 if (!System.IO.File.Exists(dbPath))
 {
  SQLiteDBHelper.CreateDB("D:\\Demo.db3");
 }
 SQLiteDBHelper db = new SQLiteDBHelper("D:\\Demo.db3");
 string sql = "CREATE TABLE Test3(id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,Name char(3),TypeName varchar(50),addDate datetime,UpdateTime Date,Time time,Comments blob)";
 db.ExecuteNonQuery(sql, null);
}
public static void InsertData()
{
 string sql = "INSERT INTO Test3(Name,TypeName,addDate,UpdateTime,Time,Comments)values(@Name,@TypeName,@addDate,@UpdateTime,@Time,@Comments)";
 SQLiteDBHelper db = new SQLiteDBHelper("D:\\Demo.db3");
 for (char c = "A"; c <= "Z"; c++)
 {
  for (int i = 0; i < 100; i++)
  {
   SQLiteParameter[] parameters = new SQLiteParameter[]{ 
           new SQLiteParameter("@Name",c+i.ToString()), 
         new SQLiteParameter("@TypeName",c.ToString()), 
         new SQLiteParameter("@addDate",DateTime.Now), 
         new SQLiteParameter("@UpdateTime",DateTime.Now.Date), 
         new SQLiteParameter("@Time",DateTime.Now.ToShortTimeString()), 
         new SQLiteParameter("@Comments","Just a Test"+i) 
         };
   db.ExecuteNonQuery(sql, parameters);
  }
 }
}
public static void ShowData()
{
 //查詢從50條起的20條記錄 
 string sql = "select * from test3 order by id desc limit 50 offset 20";
 SQLiteDBHelper db = new SQLiteDBHelper("D:\\Demo.db3");
 using (SQLiteDataReader reader = db.ExecuteReader(sql, null))
 {
  while (reader.Read())
  {
   Console.WriteLine("ID:{0},TypeName{1}", reader.GetInt64(0), reader.GetString(1));
  }
 }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

民县| 罗定市| 慈利县| 灵川县| 资源县| 双城市| 天镇县| 沁水县| 呼和浩特市| 额尔古纳市| 天镇县| 冷水江市| 平和县| 高要市| 木兰县| 和林格尔县| 鹤峰县| 额敏县| 永丰县| 紫阳县| 通许县| 开江县| 拜城县| 神池县| 海伦市| 黑山县| 津南区| 弥勒县| 达日县| 云龙县| 察雅县| 渭南市| 永宁县| 精河县| 武乡县| 从江县| 遵义市| 宁强县| 南靖县| 达孜县| 蒲江县|