ADO.NET通用DBHelper类,自己这两天琢磨的,和大家分享下,主要是希望有高手能指点下,好让我改进!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.Data.OleDb;
using System.Data.Odbc;
using System.Reflection;
namespace WebDAL
{
public class DBHelper
{
#region 属性
//项目中添加如下引用
//System.Configuration
//System.Data.OracleClient
//web.config中增加如下节点
//<appSettings>
//<add key="DBType" value="SQLServer"/>配置数据库类型SQLServer||OleDb||ODBC||Oracle
//<add key="SQLServer" value="连库字符串"/>配置该数据库类型对应的连库字符串
//<add key="assemblyName" value="Entity"/>指定实体类的命名空间
//</appSettings>
//获取连接字符串和数据库类型
//从Web.config文件中动态获取
private static string conType=ConfigurationManager.AppSettings["DBType"].ToString();
private static string constr = ConfigurationManager.AppSettings[conType].ToString();
//动态获取程序集名称,以备反射时所用
private static string assemblyName = ConfigurationManager.AppSettings["assemblyName"].ToString();
#endregion
#region 私有方法
/// <summary>
/// 根据数据库类型,获取对应数据库的连接
/// </summary>
/// <returns>连接接口</returns>
private static IDbConnection GetConnection()
{
IDbConnection con = null;
if (conType == DBType.SQLServer.ToString())
{
con = new SqlConnection(constr);
}
else if (conType == DBType.Oracle.ToString())
{
con = new OracleConnection(constr);
}
else if (conType == DBType.OleDb.ToString())
{
con = new OleDbConnection(constr);
}
else if (conType == DBType.ODBC.ToString())
{
con = new OdbcConnection(constr);
}
else
{
con = new SqlConnection(constr);
}
return con;
}
/// <summary>
/// 根据数据库类型,获取对应Command对象
/// </summary>
/// <param name="cmdText">SQL语句或存储过程名</param>
/// <param name="cmdType">SQL命令类型</param>
/// <param name="con">连接对象</param>
/// <param name="param">SQL命令参数数组</param>
/// <returns>Command接口对象</returns>
private static IDbCommand GetCommand(string commandText,CommandType commandType,IDbConnection con, params IDbDataParameter[] param)
{
IDbCommand cmd = null;
if (conType == DBType.SQLServer.ToString())
{
cmd = new SqlCommand(commandText,con as SqlConnection);
}
else if (conType == DBType.Oracle.ToString())
{
cmd = new OracleCommand(commandText, con as OracleConnection);
}
else if (conType == DBType.OleDb.ToString())
{
cmd = new OleDbCommand(commandText,con as OleDbConnection);
}
else if (conType == DBType.ODBC.ToString())
{
cmd = new OdbcCommand(commandText,con as OdbcConnection);
}
else
{
cmd = new SqlCommand(commandText, con as SqlConnection);
}
cmd.CommandType = commandType;
if (param != null)
{
cmd.Parameters.Add(param);
}
return cmd;
}
/// <summary>
/// 执行返回一条记录的泛型集合对象
/// </summary>
/// <typeparam name="T">泛型类型</typeparam>
/// <param name="reader">Reader对象,用于读取数据结果集</param>
/// <returns>泛型对象</returns>
private static T ExexuteDataReader<T>(IDataReader reader)
{
T obj = default(T);
try
{
Type type = typeof(T);
obj = (T)Assembly.Load(DBHelper.assemblyName).CreateInstance(assemblyName + "." + type.Name);
PropertyInfo[] propertyInfos = type.GetProperties();
foreach (PropertyInfo propertyinfo in propertyInfos)
{
for (int i = 0; i < reader.FieldCount; i++)
{
string filedName = reader.GetName(i);
if (filedName.ToLower() == propertyinfo.Name.ToLower())
{
Object value = reader[propertyinfo.Name];
if (value != null && value != DBNull.Value)
{
propertyinfo.SetValue(obj, value, null);
break;
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
return obj;
}
#endregion
...全文
相关推荐
封装的dbhelper
向数据集添加数据- **示例代码未给出**,通常涉及使用`SqlDataAdapter`和`DataSet`等对象,具体实现可以参考C#中的ADO.NET教程。
delphi ADO连接池
心想既然.net、java里面都有不错的连接池,那delphi应该也有吧,可是找了老半天都没能找到比较理想的。最后网上找到一个ADO的连接池实现,经过改进已在公司服务器上使用,经过半年的考验,没发现什
net操作excel
在VB.NET中操作Excel是一项常见的任务,特别是在数据处理和报告生成方面。VB.NET提供了与Microsoft Office Interop组件的集成,允许开发者直接与Excel应用程序交互。
accp s1上机考试总结
希望以上总结能够帮助大家更好地复习备考。
C#+EXCEL导入数据库SQLserver 2005+
**ADO.NET**:这是.NET框架中用于访问数据库的组件,包括 SqlConnection、SqlCommand、SqlDataAdapter 和 DataSet 等类,它们用于连接数据库、执行SQL


