namespace System.Data.SQLite
{
using System.Data;
using System.Data.SQLite;
using System.IO;
public class SqliteHelper
{
private static string pwd=”PWD”;
private static string path=Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + “\\sqliteTest.db”;
private static string connString=string.Format(“Data Source=”{0}””, path, pwd);
///
/// 返回数据库链接字符串
///
public static string ConnString
{
get { return connString; }
}
///
/// 执行SQL语句,返回受影响的行数
///
/// 需要被执行的SQL语句
///
public static int ExecuteNonQuery(string cmdText)
{
return ExecuteNonQuery(ConnString, cmdText);
}
///
/// 执行带有事务的SQL语句
///
/// 事务
/// SQL语句
///
public static int ExecuteNonQuery(SQLiteTransaction trans, string cmdText, params SQLiteParameter[] parameters)
{
int val=0;
using (SQLiteCommand cmd=new SQLiteCommand())
{
PrepareCommand(cmd, (SQLiteConnection)trans.Connection, trans, cmdText, parameters);
val=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
return val;
}
///
/// 执行SQL语句,返回受影响的行数
///
/// 连接字符串
/// SQL语句
/// SQL的参数
///
public static int ExecuteNonQuery(string connString, string cmdText, params SQLiteParameter[] parameters)
{
using (SQLiteConnection conn=new SQLiteConnection(connString))
{
return ExecuteNonQuery(conn, cmdText, parameters);
}
}
///
/// 执行SQL语句,返回受影响的行数
///
/// 数据库链接
/// SQL语句
/// 参数
///
public static int ExecuteNonQuery(SQLiteConnection connection, string cmdText, params SQLiteParameter[] parameters)
{
int val=0;
using (SQLiteCommand cmd=new SQLiteCommand())
{
PrepareCommand(cmd, connection, null, cmdText, parameters);
val=cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
}
return val;
}
///
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
///
/// SQL 语句
///
public static object ExecuteScalar(string cmdText)
{
return ExecuteScalar(ConnString, cmdText);
}
///
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
///
/// 连接字符串
/// SQL 语句
///
public static object ExecuteScalar(string connString, string cmdText)
{
using (SQLiteConnection conn=new SQLiteConnection(connString))
{
return ExecuteScalar(conn, cmdText);
}
}
///
/// 执行查询,并返回结果集的第一行的第一列.其他所有的行和列被忽略.
///
/// 数据库链接
/// SQL 语句
///
public static object ExecuteScalar(SQLiteConnection connection, string cmdText)
{
object val;
using (SQLiteCommand cmd=new SQLiteCommand())
{
PrepareCommand(cmd, connection, null, cmdText);
val=cmd.ExecuteScalar();
}
return val;
}
///
/// 执行SQL语句,返回结果集的DataReader
///
/// SQL语句
/// 参数
///
public static SQLiteDataReader ExecuteReader(string cmdText, params SQLiteParameter[] parameters)
{
return ExecuteReader(ConnString, cmdText, parameters);
}
///
/// 执行SQL语句,返回结果集的DataReader
///
/// 连接字符串
/// SQL语句
/// 参数
///
public static SQLiteDataReader ExecuteReader(string connString, string cmdText, params SQLiteParameter[] parameters)
{
SQLiteConnection conn=new SQLiteConnection(connString);
SQLiteCommand cmd=new SQLiteCommand();
try
{
PrepareCommand(cmd, conn, null, cmdText, parameters);
SQLiteDataReader rdr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch
{
conn.Close();
throw;
}
}
///
/// 预处理Command对象,数据库链接,事务,需要执行的对象,参数等的初始化
///
/// Command对象
/// Connection对象
/// Transcation对象
/// SQL Text
/// 参数实例
private static void PrepareCommand(SQLiteCommand cmd, SQLiteConnection conn, SQLiteTransaction trans, string cmdText, params SQLiteParameter[] parameters)
{
if (conn.State !=ConnectionState.Open)
conn.Open();
cmd.Connection=conn;
cmd.CommandText=cmdText;
if (trans !=null)
cmd.Transaction=trans;
if (null !=parameters && parameters.Length > 0)
{
cmd.Parameters.AddRange(parameters);
}
}
}
}