推广 热搜:   教师  系统  企业  行业  中国  公司    设备  服务 

使用工厂方法模式实现多数据库WinForm手机号码查询器(附源码)

   日期:2024-11-01     浏览:73    移动:http://lzlz0618.dbeile.cn/quote/238.html
using System;

使用工厂方法模式实现多数据库WinForm手机号码查询器(附源码)

using System.Collections.Generic;
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/ , 查看更多

特别提示:本信息由相关企业自行提供,真实性未证实,仅供参考。请谨慎采用,风险自负。


相关行业动态
推荐行业动态
点击排行
网站首页  |  关于我们  |  联系方式  |  使用协议  |  版权隐私  |  网站地图  |  排名推广  |  广告服务  |  积分换礼  |  网站留言  |  RSS订阅  |  违规举报  |  鄂ICP备2023001713号