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];
} 以上就是本篇文章【使用工厂方法模式实现多数据库WinForm手机号码查询器(附源码)】的全部内容了,欢迎阅览 ! 文章地址:http://www.dbeile.cn/quote/238.html 行业 资讯 企业新闻 行情 企业黄页 同类资讯 网站地图 返回首页 多贝乐移动站 http://lzlz0618.dbeile.cn/ , 查看更多