使用工厂方法模式实现多数据库WinForm手机号码查询器
2010-04-08 00:00:00 来源:WEB开发网Sqlite数据库操作公共库SqliteHelper全部代码如下
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SQLite;
/****************************************
* Assembly:Wang.DBUtility
* Author:walking
* E-mail:walkingp@126.com
* Homepage:http://www.51obj.cn/
* Last Modified:2010-4-2
* Description:the common database access class libary for Sqlite,all copyright served by walkingp.
* **************************************/
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;
/* return SqlConnString.ReturnConnString();*/
}
}
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];
}
}
}
更多精彩
赞助商链接