使用工厂方法模式实现多数据库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];
}
}
}
- ››使用脚本恢复WinXP系统的用户登录密码
- ››使用phpMyadmin创建数据库及独立数据库帐号
- ››使用Zend Framework框架中的Zend_Mail模块发送邮件...
- ››使用cout标准输出如何控制小数点后位数
- ››使用nofollow标签做SEO的技巧
- ››使用 WebSphere Message Broker 的 WebSphere Tra...
- ››使用SQL Server事件探查器做应用程序的性能分析
- ››使用SQL Server事件探查器分析死锁原因
- ››使用纯文本文件打造WCF服务
- ››使用 Dojo 开发定制 Business Space 小部件,第 4...
- ››使用 ADDRESS 与 INDIRECT函数查询信息
- ››使用 COLUMN函数编制单元信息
更多精彩
赞助商链接