在wince设备上,使用sqlite很方便,基本都能满足项目的需要,速度也不错。在工作中,封装了此类,方便开发。
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.sqlite;
using System.Data;
using System.IO;
namespace Austec.DB
{
public class Csqlite : Idisposable
{
public string m_LastError = null;
private string dbPath = "";
private sqliteConnection sqConn = null;
private sqliteCommand sqCmd = null;
private sqliteTransaction transaction = null;
private bool m_Result = false;
public bool Result
{
get { return m_Result; }
}
/// <summary>
/// 不允许通过该方式构造此类
/// </summary>
private Csqlite(){}
/// <summary>
/// 打开数据库
/// </summary>
/// <param name="dbPath">数据库路径</param>
public Csqlite( string dbPath )
{
this.dbPath = dbPath;
}
~Csqlite()
{
Close();
}
#region 打开与关闭
public bool open()
{
try
{
m_Result = false;
if (sqConn == null)
{
//在打开数据库时,会判断数据库是否存在,如果不存在,则在当前目录下创建一个
sqConn= new sqliteConnection("Data Source=" + dbPath +";Pooling=true;FailifMissing=false");
sqCmd = new sqliteCommand();
sqCmd.Connection = sqConn;
}
if (sqConn.State == ConnectionState.Closed)
{
bool dbExsit = System.IO.file.Exists(dbPath);
sqConn.open();
/// 如果数据库不存在,sqlite会创建一个空的数据库,在此创建一个无用的表,填充数据库
if (!dbExsit)
{
ExecuteNonquery("create table liang ( ID nvarchar(1) ) ");
}
}
m_Result = true;
return true;
}
catch (System.Exception ex)
{
m_LastError = ex.Message;
return false;
}
}
public voID dispose()
{
Close();
}
public voID Close()
{
if (sqConn != null)
{
if (sqConn.State == ConnectionState.Open)
{
sqConn.Close();
sqConn = null;
sqCmd = null;
}
}
System.Data.sqlite.sqliteConnection.ClearallPools();
}
#endregion
/// <summary>
/// 执行不带返回结果的命令
/// </summary>
/// <param name="sqlCmd">查询语句</param>
/// <returns></returns>
public bool ExecuteNonquery(string sqlCmd )
{
m_LastError = null;
m_Result = false;
try
{
sqCmd.CommandText = sqlCmd;
sqCmd.ExecuteNonquery();
m_Result = true;
}
catch (System.Exception ex)
{
m_LastError= ex.Message;
return false;
}
return true;
}
/// <summary>
/// 执行sql命令,并返回Read,Read使用完毕,必须关闭
/// </summary>
/// <param name="sqlCmd"></param>
/// <returns></returns>
public sqliteDataReader Executequery(string sqlCmd)
{
m_LastError = null;
m_Result = false;
try
{
sqCmd.CommandText = sqlCmd;
sqliteDataReader read = sqCmd.ExecuteReader();
m_Result = true;
return read;
}
catch (System.Exception ex)
{
m_LastError = ex.Message;
return null;
}
}
/// <summary>
/// 执行sql命令,必须关闭
/// </summary>
/// <param name="sqlCmd"></param>
/// <returns></returns>
public bool Executequerytable(string sqlCmd,out Datatable dt)
{
m_LastError = null;
m_Result = false;
dt = new Datatable("liang");
try
{
// 执行查询命令
sqliteDataReader read = Executequery(sqlCmd);
if ( m_Result)
{
m_Result = false;
if ( read==null )
{
m_LastError = "sqlite error:未查询到数据!";
return false;
}
/// 添充表
for( int i=0; i<read.FIEldCount;i++)
{
dt.Columns.Add(new DataColumn(i.ToString()));
}
while (read.Read())
{
DaTarow row= dt.NewRow();
for( int i=0; i<read.FIEldCount;i++ )
{
row[i]=read.GetValue(i).ToString();
}
dt.Rows.Add(row);
}
read.Close();
m_Result = true;
return true;
}
return false;
}
catch (System.Exception ex)
{
m_LastError = ex.Message;
return false;
}
}
/// <summary>
/// 执行sql命令,并返回第一行记录的第一列值
/// </summary>
/// <param name="sqlCmd"></param>
/// <returns></returns>
public object ExecuteScalar( string sqlCmd )
{
m_LastError = null;
m_Result = false;
try
{
sqCmd.CommandText = sqlCmd;
object ob= sqCmd.ExecuteScalar();
if ( ob!=null )
{
m_Result = true;
}
else
{
m_LastError = "sqlite error:未查询到数据";
}
return ob;
}
catch (System.Exception ex)
{
m_LastError = ex.Message;
return null;
}
}
/// <summary>
/// 按照内存表的结构创建表及索引;如果表已经存在,将直接返回
/// </summary>
/// <param name="dt">在创建的表</param>
/// <param name="strIndexFIEld">索引字段,多个字段以逗号分隔</param>
/// <returns></returns>
public bool Createtable(ref Datatable dt,string strIndexFIEld )
{
try
{
m_Result = false;
if ( dt==null )
{
return false;
}
// 查询表有没有存在
string sql = "select count(*) from sqlite_master where tbl_name='" + dt.tablename + "' and type='table'";
object ob = ExecuteScalar(sql);
if (!m_Result)
{
return false;
}
if (Convert.ToInt32(ob) == 1)
{
m_Result = true;
return true;
}
string strsql="",strCmdsql="";
strCmdsql = " CREATE table " + dt.tablename + " ( ";
for (int i = 0; i < dt.Columns.Count; i++ )
{
strsql += dt.Columns[i].Columnname + " nvarchar(30) ";
if ( i<dt.Columns.Count-1)
{
strsql += ",";
}
}
strCmdsql += strsql + " )";
if (!ExecuteNonquery(strCmdsql))
{
return false;
}
if ( strIndexFIEld!=null && strIndexFIEld.Trim().Length>0 )
{
string[] strIndex = strIndexFIEld.Split(',');
for (int i = 0; i < strIndex.Length; i++)
{
strCmdsql = "CREATE INDEX " + dt.tablename + "_index" + i.ToString() +" ON " + dt.tablename + " ( " + strIndex[i] + " )";
if (!ExecuteNonquery(strCmdsql))
{
return false;
}
}
}
m_Result = true;
return true;
}
catch (System.Exception ex)
{
m_LastError = ex.Message;
return false;
}
}
/// <summary>
/// 删除表及表的索引
/// </summary>
/// <param name="tablename"></param>
/// <returns></returns>
public bool DroptableAndindex( string tablename )
{
m_Result = false;
try
{
// 查询表及索引有没有存在
string sql = "select count(*) from sqlite_master where tbl_name='" + tablename + "' and type='table'";
object ob= ExecuteScalar(sql);
if (!m_Result)
{
return false;
}
if ( Convert.ToInt32(ob)==0 )
{
m_Result=true;
return true;
}
string cmdsql = "drop table " + tablename ;
if ( !ExecuteNonquery(cmdsql) )
{
return false;
}
m_Result = true;
return true;
}
catch (System.Exception ex)
{
m_LastError = ex.Message;
return false;
}
}
/// <summary>
/// 将内存表插入相应的表中
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
public bool Inserttable( ref Datatable dt)
{
m_Result = false;
if (dt == null)
{
return false;
}
if ( dt.Rows.Count==0 )
{
m_Result = true;
return true;
}
int i = 0;
string sql = "insert into " + dt.tablename + " values( ";
string sqlData = "",strCmdsql = "";
BeginTransaction();
foreach (DaTarow row in dt.Rows)
{
sqlData = "";
for (i = 0; i < dt.Columns.Count; i++)
{
sqlData += "'" + row[i].ToString() + "'";
if (i < dt.Columns.Count - 1)
{
sqlData += ",";
}
}
strCmdsql = sql + sqlData + " )";
if (!ExecuteNonquery(strCmdsql))
{
Rollback();
return false;
}
}
Commit();
m_Result = true;
return true;
}
/// <summary>
/// 删除指定表的数据
/// </summary>
/// <param name="tablename">表名</param>
/// <returns></returns>
public bool DeletetableData( string tablename )
{
m_Result = false;
string sql = "select count(*) from sqlite_master where tbl_name='" + tablename + "' and type='table'";
object ob= ExecuteScalar(sql);
if (!m_Result)
{
return false;
}
m_Result = false;
if ( Convert.ToInt32(ob)==0 )
{
m_LastError = "被删除数据的表不存在!";
return false;
}
sql = "delete from " + tablename;
if ( ExecuteNonquery(sql) )
{
m_Result = true;
return true;
}
else
{
return false;
}
}
#region 事务 *** 作
public voID BeginTransaction()
{
transaction = sqConn.BeginTransaction();
}
public voID Commit()
{
transaction.Commit();
}
public voID Rollback() { transaction.Rollback(); } #endregion }}
总结以上是内存溢出为你收集整理的 *** 作 sqlite封装的一个类库全部内容,希望文章能够帮你解决 *** 作 sqlite封装的一个类库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)