【文件属性】:
文件名称:超牛×的DBHelper类
文件大小:13KB
文件格式:CS
更新时间:2012-11-07 16:03:49
DBHelper
using System.Text;
using System.Data.Common;
using System.Data;
using System.Configuration;
namespace HomeAndSchool.DAL
{
public class DBHelper
{
private static string dbProviderName = ConfigurationManager.AppSettings["DbHelperProvider"];
private static string dbConnectionString = ConfigurationManager.ConnectionStrings["DbHelperConnectionString"].ToString();
private DbConnection connection;
public DBHelper()
{
this.connection = CreateConnection(DBHelper.dbConnectionString);
}
public DBHelper(string connectionString)
{
this.connection = CreateConnection(connectionString);
}
///
/// 获得DbConnection
///
///
public static DbConnection CreateConnection()
{
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
DbConnection dbconn = dbfactory.CreateConnection();
dbconn.ConnectionString = DBHelper.dbConnectionString;
return dbconn;
}
///
/// 获得DbConnection
///
/// connectionString
///
public static DbConnection CreateConnection(string connectionString)
{
DbProviderFactory dbfactory = DbProviderFactories.GetFactory(DBHelper.dbProviderName);
DbConnection dbconn = dbfactory.CreateConnection();
dbconn.ConnectionString = connectionString;
return dbconn;
}
///
/// 执行的存储过程
///
///
///
public DbCommand GetStoredProcCommond(string storedProcedure)
{
DbCommand dbCommand = connection.CreateCommand();
dbCommand.CommandText = storedProcedure;
dbCommand.CommandType = CommandType.StoredProcedure;
return dbCommand;
}
///
/// 执行的sql语句
///
///
///
public DbCommand GetSqlStringCommond(string sqlQuery)
{
DbCommand dbCommand = connection.CreateCommand();
dbCommand.CommandText = sqlQuery;
dbCommand.CommandType = CommandType.Text;
return dbCommand;
}
//增加输入参数#region 增加参数
public void AddParameterCollection(DbCommand cmd, DbParameterCollection dbParameterCollection)
{
foreach (DbParameter dbParameter in dbParameterCollection)
{
cmd.Parameters.Add(dbParameter);
}
}
///
/// 增加输出参数 适用于存储过程
///
/// cmd对象
/// 参数名
/// 参数类型
/// 参数大小
public void AddOutParameter(DbCommand cmd, string parameterName, DbType dbType, int size)
{
DbParameter dbParameter = cmd.CreateParameter();
dbParameter.DbType = dbType;
dbParameter.ParameterName = parameterName;
dbParameter.Size = size;
dbParameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(dbParameter);
}
///
/// 增加参数列表
///
/// CMD对象
/// 参数名
/// 参数类型
/// 参数值
.....