using System.Text;
using System.Data;
using System.Data.SQLite;
namespace Wang.DBUtility
{
/// <summary>
/// Sqlite数据操作公共类
/// </summary>
public static class SqliteHelper
{
/// <summary>
/// 连接字符串
/// </summary>
public static string ConnStr
{
get
{
//调试时使用
string appPath = AppDomain.CurrentDomain.SetupInformation.Applicationbase;
int index=AppDomain.CurrentDomain.SetupInformation.Applicationbase.LastIndexOf("\");
appPath = "Data Source=" + appPath.Substring(0, index);
appPath = appPath.Substring(0, appPath.LastIndexOf("\"));
appPath = appPath.Substring(0, appPath.LastIndexOf("\"));
appPath += System.Configuration.ConfigurationManager.AppSettings["SqliteFile"];
string _conn = appPath;
return _conn;
}
}
private static SQLiteConnection _conn;
/// <summary>
/// SQLiteConnection
/// </summary>
public static SQLiteConnection Conn
{
get
{
SQLiteConnection conn = new SQLiteConnection(ConnStr);
conn.Open();
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
else if (conn.State == ConnectionState.Broken)
{
conn.Close();
conn.Open();
}
return conn;
}
set { _conn = value; }
}
/// <summary>
/// 关闭连接
/// </summary>
public static void ConnClose()
{
if (Conn.State != ConnectionState.Closed)
Conn.Close();
}
/// <summary>
/// 执行SQL
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static int ExecuteCommand(string strSql)
{
SQLiteCommand cmd = new SQLiteCommand(strSql, Conn);
int i = cmd.ExecuteNonQuery();
return i;
}
/// <summary>
/// 带参数的执行SQL
/// </summary>
/// <param name="strSql"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static int ExecuteCommand(string strSql, params SQLiteParameter[] paras)
{
SQLiteCommand cmd = new SQLiteCommand(strSql, Conn);
cmd.Parameters.AddRange(paras);
int i = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return i;
}
/// <summary>
/// 返回一行记录
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static int GetScalar(string strSql)
{
SQLiteCommand cmd = new SQLiteCommand(strSql, Conn);
int i = Convert.ToInt32(cmd.ExecuteScalar());
return i;
}
/// <summary>
/// 返回一行记录
/// </summary>
/// <param name="strSql"></param>
/// <param name="paras"></param>
/// <returns></returns>
public static int GetScalar(string strSql, params SQLiteParameter[] paras)
{
SQLiteCommand cmd = new SQLiteCommand(strSql, Conn);
cmd.Parameters.AddRange(paras);
int i = Convert.ToInt32(cmd.ExecuteScalar());
cmd.Parameters.Clear();
return i;
}
/// <summary>
/// 返回SQLiteDataReader
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static SQLiteDataReader GetReader(string strSql)
{
SQLiteCommand cmd = new SQLiteCommand(strSql, Conn);
SQLiteDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
return dr;
}
/// <summary>
/// 带参数返回SQLiteDataReader
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static SQLiteDataReader GetReader(string strSql, params SQLiteParameter[] paras)
{
SQLiteCommand cmd = new SQLiteCommand(strSql, Conn);
cmd.Parameters.AddRange(paras);
SQLiteDataReader dr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return dr;
}
/// <summary>
/// 返回DataTable
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static DataTable GetDataTable(string strSql)
{
SQLiteCommand cmd = new SQLiteCommand(strSql, Conn);
DataSet ds = new DataSet();
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
ds.Clear();
da.Fill(ds);
return ds.Tables[0];
}
/// <summary>
/// 带参数返回DataTable
/// </summary>
/// <param name="strSql"></param>
/// <returns></returns>
public static DataTable GetDataTable(string strSql, params SQLiteParameter[] paras)
{
SQLiteCommand cmd = new SQLiteCommand(strSql, Conn);
cmd.Parameters.AddRange(paras);
SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
DataSet ds = new DataSet();
ds.Clear();
da.Fill(ds);
return ds.Tables[0];
} 本文地址:http://www.dbeile.cn/quote/238.html 多贝乐 http://www.dbeile.cn/ , 查看更多