当前位置:数据库 > SQLite >>

C# 使用sqlite 轻量级数据库

 

一 准备工作

 

sqlite3.exe 下载地址:http://www.sqlite.org/download.html    下载"sqlite-shell-win32-x86-

 

3070800.zip" 就OK了Precompiled Binaries For Windows 

 

sqlite-shell-win32-x86-3070800.zip

(248.28 KiB)

 

 

 

 

system.data.sqlite.dll 下载地址: http://www.dllzj.com/Down_System.Data.SQLite.DLL.html  这个dll

 

用于Visual Studio 项目中引用

 

 

 

二,试用sqlite3.exe

 

 

 

解压sqlite-shell-win32-x86-3070800.zip  到F:\jonse\DownLoads\sqlLite 目录下

 

开始-->运行-->cmd

 

>F:

 

>cd F:\jonse\DownLoads\sqlLite

 

>sqlite3 myDB.db    (如果myDB.db不存在,则创建之;若存在,则打开它)

 

>create table test(id int,name varchar(20),remark varchar(200));   (创建test表,三列:

 

id,name,remark)

 

>insert into test select 1,'name1','remark1' union select 2,'name2','remark2'; (暂时插入2行数据)

 

>.mode column (显示列模式)

 

>.headers on (显示列头信息)

 

>select * from test; (查询所有的数据)

 

>select ifnull(max(id),0) as MaxID from test; (查询test表最大的ID值)

 

 

 

 

 

三, C# 使用sqlite

 

 

 

在VS2010的项目引用中添加System.Data.SQLite.dll (这个在准备工作中已经下载好的)

 

 

 

(1),新建一个SqlLiteHelper.cs 的类

 

 

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

using System.Data;

using System.Data.SQLite;

using System.Data.Common;

 

namespace JonseTest

{

   public abstract class SqlLiteHelper

    {

       public static string ConnSqlLiteDbPath = string.Empty;

       public static string ConnString

       {

           get

           {

               return string.Format(@"Data Source={0}", ConnSqlLiteDbPath);

           }

       }

 

       // 取datatable

       public static DataTable GetDataTable(out string sError,string sSQL)

       {

           DataTable dt = null;

           sError = string.Empty;

 

           try

           {

               SQLiteConnection conn = new SQLiteConnection(ConnString);

               conn.Open();

               SQLiteCommand cmd = new SQLiteCommand();

               cmd.CommandText = sSQL;

               cmd.Connection = conn;

               SQLiteDataAdapter dao = new SQLiteDataAdapter(cmd);

               dt = new DataTable();

               dao.Fill(dt);

           }

           catch (Exception ex)

           {

               sError = ex.Message;

           }

 

           return dt;

       }

 

       // 取某个单一的元素

       public static object GetSingle(out string sError, string sSQL)

       {

           DataTable dt = GetDataTable(out sError, sSQL);

           if (dt != null && dt.Rows.Count > 0)

           {

               return dt.Rows[0][0];

           }

 

           return null;

       }

 

       // 取最大的ID

       public static Int32 GetMaxID(out string sError, string sKeyField,string sTableName)

       {

           DataTable dt = GetDataTable(out sError, "select ifnull(max([" + sKeyField + "]),0) as

 

MaxID from [" + sTableName + "]");

           if (dt != null && dt.Rows.Count > 0)

           {

               return Convert.ToInt32(dt.Rows[0][0].ToString());

           }

 

           return 0;

       }

 

       // 执行insert,update,delete 动作,也可以使用事务

       public static bool UpdateData(out string sError, string sSQL,bool bUseTransaction=false)

       {

           int iResult = 0;

           sError = string.Empty;

 

           if (!bUseTransaction)

           {

               try

               {

 &nbs

补充:软件开发 , C# ,
Oracle
MySQL
Access
SQLServer
DB2
Excel
SQLite
SYBASE
Postgres
如果你遇到数据库难题:
请访问www.zzzyk.com 试试
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,