说明:此篇文章是给那些和我一样仍在使用ADO.NET访问数据库的.NET开发人员写的,因为某些原因,比如还在使用.NET3.0以下版本开发.NET应用或者所使用的数据库对ORM支持不是很好,或者是对ORM的性能有所怀疑(其实在访问量不是很大的情况下没有必要过分追求性能的极限)等等,这部分人仍在使用传统的ADO.NET来访问数据库,他们或手写或采用代码生成工具来生成实体类和增删改查的SQL语句,在将DataTable或者DataReader转换成对应的实体类的时候仍需要一行行写代码,本类就是为了解决这个问题的,可以用几个类来实现方便快捷的转换。本类库在SQL Server/MySQL/SQLite下测试通过,由于条件有限未在所有的数据库下测试,如果有问题请在此留言或者在周公的微博留言(http://weibo.com/zhoufoxcn)。
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.Common;
- namespace NetSkycn.Data
- {
- /// <summary>
- /// 通用数据库访问类,封装了对数据库的常见操作
- /// 作者:周公
- /// 创建日期:2011-07-18
- /// 修改日期:2012-04-12
- /// 新浪微博地址:http://weibo.com/zhoufoxcn
- /// </summary>
- public sealed class DbUtility
- {
- public string ConnectionString { get; set; }
- private DbProviderFactory providerFactory;
- /// <summary>
- /// 构造函数
- /// </summary>
- /// <param name="connectionString">数据库连接字符串</param>
- /// <param name="providerType">数据库类型枚举,参见<paramref name="providerType"/></param>
- public DbUtility(string connectionString, DbProviderType providerType)
- {
- ConnectionString = connectionString;
- providerFactory = ProviderFactory.GetDbProviderFactory(providerType);
- if (providerFactory == null)
- {
- throw new ArgumentException("Can't load DbProviderFactory for given value of providerType");
- }
- }
- /// <summary>
- /// 对数据库执行增删改操作,返回受影响的行数。
- /// </summary>
- /// <param name="sql">要执行的增删改的SQL语句</param>
- /// <param name="parameters">执行增删改语句所需要的参数</param>
- /// <returns></returns>
- public int ExecuteNonQuery(string sql, IList<DbParameter> parameters)
- {
- return ExecuteNonQuery(sql, parameters, CommandType.Text);
- }
- /// <summary>
- /// 对数据库执行增删改操作,返回受影响的行数。
- /// </summary>
- /// <param name="sql">要执行的增删改的SQL语句</param>
- /// <param name="parameters">执行增删改语句所需要的参数</param>
- /// <param name="commandType">执行的SQL语句的类型</param>
- /// <returns></returns>
- public int ExecuteNonQuery(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- command.Connection.Open();
- int affectedRows = command.ExecuteNonQuery();
- command.Connection.Close();
- return affectedRows;
- }
- }
- /// <summary>
- /// 执行一个查询语句,返回一个关联的DataReader实例
- /// </summary>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <returns></returns>
- public DbDataReader ExecuteReader(string sql, IList<DbParameter> parameters)
- {
- return ExecuteReader(sql, parameters, CommandType.Text);
- }
- /// <summary>
- /// 执行一个查询语句,返回一个关联的DataReader实例
- /// </summary>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <param name="commandType">执行的SQL语句的类型</param>
- /// <returns></returns>
- public DbDataReader ExecuteReader(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- DbCommand command = CreateDbCommand(sql, parameters, commandType);
- command.Connection.Open();
- return command.ExecuteReader(CommandBehavior.CloseConnection);
- }
- /// <summary>
- /// 执行一个查询语句,返回一个包含查询结果的DataTable
- /// </summary>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <returns></returns>
- public DataTable ExecuteDataTable(string sql, IList<DbParameter> parameters)
- {
- return ExecuteDataTable(sql, parameters, CommandType.Text);
- }
- /// <summary>
- /// 执行一个查询语句,返回一个包含查询结果的DataTable
- /// </summary>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <param name="commandType">执行的SQL语句的类型</param>
- /// <returns></returns>
- public DataTable ExecuteDataTable(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- using (DbDataAdapter adapter = providerFactory.CreateDataAdapter())
- {
- adapter.SelectCommand = command;
- DataTable data = new DataTable();
- adapter.Fill(data);
- return data;
- }
- }
- }
- /// <summary>
- /// 执行一个查询语句,返回查询结果的第一行第一列
- /// </summary>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <returns></returns>
- public Object ExecuteScalar(string sql, IList<DbParameter> parameters)
- {
- return ExecuteScalar(sql, parameters, CommandType.Text);
- }
- /// <summary>
- /// 执行一个查询语句,返回查询结果的第一行第一列
- /// </summary>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <param name="commandType">执行的SQL语句的类型</param>
- /// <returns></returns>
- public Object ExecuteScalar(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- using (DbCommand command = CreateDbCommand(sql, parameters, commandType))
- {
- command.Connection.Open();
- object result = command.ExecuteScalar();
- command.Connection.Close();
- return result;
- }
- }
- /// <summary>
- /// 查询多个实体集合
- /// </summary>
- /// <typeparam name="T">返回的实体集合类型</typeparam>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <returns></returns>
- public List<T> QueryForList<T>(string sql, IList<DbParameter> parameters) where T : new()
- {
- return QueryForList<T>(sql, parameters, CommandType.Text);
- }
- /// <summary>
- /// 查询多个实体集合
- /// </summary>
- /// <typeparam name="T">返回的实体集合类型</typeparam>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <param name="commandType">执行的SQL语句的类型</param>
- /// <returns></returns>
- public List<T> QueryForList<T>(string sql, IList<DbParameter> parameters, CommandType commandType) where T : new()
- {
- DataTable data = ExecuteDataTable(sql, parameters, commandType);
- return EntityReader.GetEntities<T>(data);
- }
- /// <summary>
- /// 查询单个实体
- /// </summary>
- /// <typeparam name="T">返回的实体集合类型</typeparam>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <returns></returns>
- public T QueryForObject<T>(string sql, IList<DbParameter> parameters) where T : new()
- {
- return QueryForObject<T>(sql, parameters, CommandType.Text);
- }
- /// <summary>
- /// 查询单个实体
- /// </summary>
- /// <typeparam name="T">返回的实体集合类型</typeparam>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <param name="commandType">执行的SQL语句的类型</param>
- /// <returns></returns>
- public T QueryForObject<T>(string sql, IList<DbParameter> parameters, CommandType commandType) where T : new()
- {
- return QueryForList<T>(sql, parameters, commandType)[0];
- }
- public DbParameter CreateDbParameter(string name, object value)
- {
- return CreateDbParameter(name, ParameterDirection.Input, value);
- }
- public DbParameter CreateDbParameter(string name, ParameterDirection parameterDirection, object value)
- {
- DbParameter parameter = providerFactory.CreateParameter();
- parameter.ParameterName = name;
- parameter.Value = value;
- parameter.Direction = parameterDirection;
- return parameter;
- }
- /// <summary>
- /// 创建一个DbCommand对象
- /// </summary>
- /// <param name="sql">要执行的查询语句</param>
- /// <param name="parameters">执行SQL查询语句所需要的参数</param>
- /// <param name="commandType">执行的SQL语句的类型</param>
- /// <returns></returns>
- private DbCommand CreateDbCommand(string sql, IList<DbParameter> parameters, CommandType commandType)
- {
- DbConnection connection = providerFactory.CreateConnection();
- DbCommand command = providerFactory.CreateCommand();
- connection.ConnectionString = ConnectionString;
- command.CommandText = sql;
- command.CommandType = commandType;
- command.Connection = connection;
- if (!(parameters == null || parameters.Count == 0))
- {
- foreach (DbParameter parameter in parameters)
- {
- command.Parameters.Add(parameter);
- }
- }
- return command;
- }
- }
- /// <summary>
- /// 数据库类型枚举
- /// </summary>
- public enum DbProviderType : byte
- {
- SqlServer,
- MySql,
- SQLite,
- Oracle,
- ODBC,
- OleDb,
- Firebird,
- PostgreSql,
- DB2,
- Informix,
- SqlServerCe
- }
- /// <summary>
- /// DbProviderFactory工厂类
- /// </summary>
- public class ProviderFactory
- {
- private static Dictionary<DbProviderType, string> providerInvariantNames = new Dictionary<DbProviderType, string>();
- private static Dictionary<DbProviderType, DbProviderFactory> providerFactoies = new Dictionary<DbProviderType, DbProviderFactory>(20);
- static ProviderFactory()
- {
- //加载已知的数据库访问类的程序集
- providerInvariantNames.Add(DbProviderType.SqlServer, "System.Data.SqlClient");
- providerInvariantNames.Add(DbProviderType.OleDb, "System.Data.OleDb");
- providerInvariantNames.Add(DbProviderType.ODBC, "System.Data.ODBC");
- providerInvariantNames.Add(DbProviderType.Oracle, "Oracle.DataAccess.Client");
- providerInvariantNames.Add(DbProviderType.MySql, "MySql.Data.MySqlClient");
- providerInvariantNames.Add(DbProviderType.SQLite, "System.Data.SQLite");
- providerInvariantNames.Add(DbProviderType.Firebird, "FirebirdSql.Data.Firebird");
- providerInvariantNames.Add(DbProviderType.PostgreSql, "Npgsql");
- providerInvariantNames.Add(DbProviderType.DB2, "IBM.Data.DB2.iSeries");
- providerInvariantNames.Add(DbProviderType.Informix, "IBM.Data.Informix");
- providerInvariantNames.Add(DbProviderType.SqlServerCe, "System.Data.SqlServerCe");
- }
- /// <summary>
- /// 获取指定数据库类型对应的程序集名称
- /// </summary>
- /// <param name="providerType">数据库类型枚举</param>
- /// <returns></returns>
- public static string GetProviderInvariantName(DbProviderType providerType)
- {
- return providerInvariantNames[providerType];
- }
- /// <summary>
- /// 获取指定类型的数据库对应的DbProviderFactory
- /// </summary>
- /// <param name="providerType">数据库类型枚举</param>
- /// <returns></returns>
- public static DbProviderFactory GetDbProviderFactory(DbProviderType providerType)
- {
- //如果还没有加载,则加载该DbProviderFactory
- if (!providerFactoies.ContainsKey(providerType))
- {
- providerFactoies.Add(providerType, ImportDbProviderFactory(providerType));
- }
- return providerFactoies[providerType];
- }
- /// <summary>
- /// 加载指定数据库类型的DbProviderFactory
- /// </summary>
- /// <param name="providerType">数据库类型枚举</param>
- /// <returns></returns>
- private static DbProviderFactory ImportDbProviderFactory(DbProviderType providerType)
- {
- string providerName = providerInvariantNames[providerType];
- DbProviderFactory factory = null;
- try
- {
- //从全局程序集中查找
- factory = DbProviderFactories.GetFactory(providerName);
- }
- catch (ArgumentException e)
- {
- factory = null;
- }
- return factory;
- }
- }
- }
------------------------------------------------
广告:为便于武汉市的.NET开发人员和学生参加在武汉举办的学习交流活动(本人绝没有地域歧视,只为线下交流方便),请有意者加以下QQ群:武汉IT群(11690964),武汉微软移动俱乐部(198027326)。4月14日 本周六 13:30 - 17:00就有一个有关WPhone的免费线下交流活动,地点在武汉光谷软件园DEMO咖啡屋。如果有兴趣请在新浪微博上报名,报名地址:http://event.weibo.com/405882