using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Common; using System.Data; using System.Reflection; using Common.AssemblyUtil; namespace Common.Data { /// /// 通用数据库访问类,封装了对数据库的常见操作 /// public sealed class DbUtility { public string ConnectionString { get; set; } private DbProviderFactory providerFactory; /// /// 构造函数 /// /// 数据库连接字符串 /// 数据库类型枚举,参见 public DbUtility(string connectionString, DbProviderType providerType) { ConnectionString = connectionString; providerFactory = ProviderFactory.GetDbProviderFactory(providerType); if (providerFactory == null) { throw new ArgumentException("无法确定数据源类型"); } } /// /// 对数据库执行增删改操作,返回受影响的行数。 /// /// 要执行的增删改的SQL语句 /// 执行增删改语句所需要的参数 /// public int ExecuteNonQuery(string sql, IList parameters) { return ExecuteNonQuery(sql, parameters, System.Data.CommandType.Text); } /// /// 对数据库执行增删改操作,返回受影响的行数。 /// /// 要执行的增删改的SQL语句 /// 执行增删改语句所需要的参数 /// 执行的SQL语句的类型 /// public int ExecuteNonQuery(string sql, IList parameters, System.Data.CommandType commandType) { using (DbCommand command = CreateDbCommand(sql, parameters, commandType)) { command.Connection.Open(); int affectedRows = command.ExecuteNonQuery(); command.Connection.Close(); return affectedRows; } } /// /// 执行一个查询语句,返回一个关联的DataReader实例 /// /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// private DbDataReader ExecuteReader(string sql, IList parameters) { return ExecuteReader(sql, parameters, System.Data.CommandType.Text); } /// /// 执行一个查询语句,返回一个关联的DataReader实例 /// /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// 执行的SQL语句的类型 /// private DbDataReader ExecuteReader(string sql, IList parameters, System.Data.CommandType commandType) { DbCommand command = CreateDbCommand(sql, parameters, commandType); command.Connection.Open(); return command.ExecuteReader(CommandBehavior.CloseConnection); } /// /// 执行一个查询语句,返回一个包含查询结果的DataTable /// /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// private DataTable ExecuteDataTable(string sql, IList parameters) { return ExecuteDataTable(sql, parameters, System.Data.CommandType.Text); } /// /// 执行一个查询语句,返回一个包含查询结果的DataTable /// /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// 执行的SQL语句的类型 /// private DataTable ExecuteDataTable(string sql, IList parameters, System.Data.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; } } } /// /// 执行一个查询语句,返回查询结果的第一行第一列 /// /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// public Object ExecuteScalar(string sql, IList parameters) { return ExecuteScalar(sql, parameters, System.Data.CommandType.Text); } /// /// 执行一个查询语句,返回查询结果的第一行第一列 /// /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// 执行的SQL语句的类型 /// public Object ExecuteScalar(string sql, IList parameters, System.Data.CommandType commandType) { using (DbCommand command = CreateDbCommand(sql, parameters, commandType)) { command.Connection.Open(); object result = command.ExecuteScalar(); command.Connection.Close(); return result; } } /// /// 查询多个实体集合 /// /// 返回的实体集合类型 /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// public List QueryForList(string sql, IList parameters) where T : new() { return QueryForList(sql, parameters, System.Data.CommandType.Text); } /// ///查询多个实体集合 /// /// 返回的实体集合类型 /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// 执行的SQL语句的类型 /// public List QueryForList(string sql, IList parameters, System.Data.CommandType commandType) where T : new() { DataTable data = ExecuteDataTable(sql, parameters, commandType); return EntityFactory.GetEntities(data); } /// /// 查询单个实体 /// /// 返回的实体集合类型 /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// public T QueryForObject(string sql, IList parameters) where T : new() { return QueryForObject(sql, parameters, System.Data.CommandType.Text); } /// /// 查询单个实体 /// /// 返回的实体集合类型 /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// 执行的SQL语句的类型 /// public T QueryForObject(string sql, IList parameters, System.Data.CommandType commandType) where T : new() { return QueryForList(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; } /// /// 创建一个DbCommand对象 /// /// 要执行的查询语句 /// 执行SQL查询语句所需要的参数 /// 执行的SQL语句的类型 /// private DbCommand CreateDbCommand(string sql, IList parameters, System.Data.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; } } /// /// 数据库类型枚举 /// public enum DbProviderType : byte { SqlServer, MySql, SQLite, Oracle, ODBC, OleDb, Firebird, PostgreSql, DB2, Informix, SqlServerCe } /// /// DbProviderFactory工厂类 /// public class ProviderFactory { private static Dictionary providerInvariantNames = new Dictionary(); private static Dictionary providerFactoies = new Dictionary(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"); } /// /// 获取指定数据库类型对应的程序集名称 /// /// 数据库类型枚举 /// public static string GetProviderInvariantName(DbProviderType providerType) { return providerInvariantNames[providerType]; } /// /// 获取指定类型的数据库对应的DbProviderFactory /// /// 数据库类型枚举 /// public static DbProviderFactory GetDbProviderFactory(DbProviderType providerType) { //如果还没有加载,则加载该DbProviderFactory if (!providerFactoies.ContainsKey(providerType)) { providerFactoies.Add(providerType, ImportDbProviderFactory(providerType)); } return providerFactoies[providerType]; } /// /// 加载指定数据库类型的DbProviderFactory /// /// 数据库类型枚举 /// 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; } } /// /// 自定义属性,用于指示如何从DataTable或者DbDataReader中读取类的属性值 /// public class ColumnNameAttribute : Attribute { /// /// 类属性对应的列名 /// public string ColumnName { get; set; } /// /// 构造函数 /// /// 类属性对应的列名 public ColumnNameAttribute(string columnName) { ColumnName = columnName; } } }