代码
/*
* 作者: 牛腩
* 创建时间: 2010-1-20 10:43:14
* Email: 164423073@qq.com
* 说明: 对SQLite数据库的增删查改操作的封装类
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SQLite;
using System.Web.Configuration;
using System.Web;
namespace Niunan.Tg029.Hr.Utility
{
public class SQLiteHelper
{
private SQLiteConnection conn = null;
private SQLiteCommand cmd = null;
private SQLiteDataReader sdr = null;
public SQLiteHelper()
{
//string connStr = WebConfigurationManager.ConnectionStrings["connStr"].ToString();
string connStr = "Data Source=" + HttpContext.Current.Server.MapPath("~/test/test.sqlite") ;
conn = new SQLiteConnection(connStr);
}
/// <summary>创建Command对象
///
/// </summary>
/// <param name="sql">SQL语句</param>
public void CreateCommand(string sql) {
conn.Open();
cmd = new SQLiteCommand(sql, conn);
}
/// <summary>添加参数
///
/// </summary>
/// <param name="paramName">参数名称</param>
/// <param name="value">值</param>
public void AddParameter(string paramName,object value) {
cmd.Parameters.Add(new SQLiteParameter(paramName,value));
}
/// <summary>执行不带参数的增删改SQL语句
///
/// </summary>
/// <param name="cmdText">增删改SQL语句</param>
/// <param name="ct">命令类型</param>
/// <returns></returns>
public bool ExecuteNonQuery()
{
int res;
try
{
res = cmd.ExecuteNonQuery();
if (res > 0)
{
return true;
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return false;
}
/// <summary>执行查询SQL语句
///
/// </summary>
/// <param name="cmdText">查询SQL语句</param>
/// <returns></returns>
public DataTable ExecuteQuery()
{
DataTable dt = new DataTable();
using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
{
dt.Load(sdr);
}
return dt;
}
/// <summary>返回查询SQL语句查询出的结果的第一行第一列的值
///
/// </summary>
/// <returns></returns>
public string ExecuteScalar()
{
string res = "";
try
{
object obj = cmd.ExecuteScalar();
if (obj != null)
{
res = obj.ToString();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
}
return res;
}
}
}
使用示例看附件吧,蛮简单的一个封装,特此记录,以备不时之需!!!
附件下载:http://niunan.net/download/sqlitetest.7z