SQLHelper帮助类_下(支持多数据库的封装)
在上篇关于SQLHelper类中,主要针对SQLServer数据库进行的。在使用别的数据库,就要修改部分代码!所以今天就写一个支持多数据库的封装!主要用到枚举,读取config文件!接口的简单用法。获取连接等...
一.config配置文件:
<connectionStrings> <add name="constr" connectionString="server=127.0.0.1;user id=用户名;password=密码;database=数据库名"/> </connectionStrings> <appSettings> <add key="database" value ="MYSQL"/> </appSettings>
关于连接字符串在SQLHelper帮助类_ 上 进行介绍 http://www.cnblogs.com/fengxuehuanlin/p/5271944.html
二.SqlHelper:
1 private static string connstr; 2 /// <summary> 3 /// 从配置文件中获取连接字符串名,使用什么数据库 4 /// <appSettings>中key名:database,value值:数据库 5 /// <connectionStrings>name:constr 6 /// </summary> 7 static SqlHelper() 8 { 9 connstr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString; //获取config文件中连接字符串 10 string databaseType = ConfigurationManager.AppSettings["database"].ToUpper(); 11 DBdatabase.database = AdoHelper.EnumParse<DatabaseList>(databaseType); //将字符串转换成枚举类型 12 } 13 14 /// <summary> 15 /// 获取相应数据库的连接 16 /// </summary> 17 /// <returns></returns> 18 public static IDbConnection getConnection() 19 { 20 string value = DBdatabase.database.ToString(); 21 DatabaseList database= AdoHelper.EnumParse<DatabaseList>(value); 22 switch(database) 23 { 24 case DatabaseList.MYSQL: 25 return new MySqlConnection(connstr); 26 case DatabaseList.SQLSERVER: 27 return new SqlConnection(connstr); 28 default: 29 throw new Exception("数据库类型目前不支持!"); 30 } 31 } 32 33 /// <summary> 34 /// 将数据加载到本地,在本地对数据进行操作 35 /// </summary> 36 /// <param name="Sql">Sql语句</param> 37 /// <param name="parameter">参数化查询</param> 38 /// <returns>返回从数据库中读取到的DataTable表</returns> 39 public static DataTable ExecuteQuery(string Sql, params IDbDataParameter[] parameter) 40 { 41 using (IDbConnection conn=getConnection()) 42 using (IDbCommand cmd = conn.CreateCommand()) 43 { 44 conn.Open(); 45 cmd.CommandText = Sql; 46 for (int i = 0; i < parameter.Length; i++) 47 { 48 cmd.Parameters.Add(parameter[i]); 49 } 50 DataTable tab = new DataTable(); 51 using (IDataReader reader = cmd.ExecuteReader()) 52 { 53 tab.Load(reader); 54 return tab; 55 } 56 } 57 } 58 /// <summary> 59 /// 用于执行增加和删除语句 60 /// </summary> 61 /// <param name="Sql">Sql语句</param> 62 /// <param name="parameter">参数化查询</param> 63 /// <returns>有多少语句执行成功</returns> 64 public static int ExecuteNonQuery(string Sql, params IDbDataParameter[] parameter) 65 { 66 using (IDbConnection conn = getConnection()) 67 using (IDbCommand cmd = conn.CreateCommand()) 68 { 69 conn.Open(); 70 cmd.CommandText = Sql; 71 for (int i = 0; i < parameter.Length; i++) 72 { 73 cmd.Parameters.Add(parameter[i]); 74 } 75 return cmd.ExecuteNonQuery(); 76 } 77 } 78 /// <summary> 79 /// 执行语句后,返回第一行第一列的数据 80 /// </summary> 81 /// <param name="Sql"></param> 82 /// <param name="parameter"></param> 83 /// <returns>object类型的值</returns> 84 public static object ExecuteScalar(string Sql, params IDbDataParameter[] parameter) 85 { 86 using (IDbConnection conn=getConnection()) 87 using (IDbCommand cmd = conn.CreateCommand()) 88 { 89 conn.Open(); 90 cmd.CommandText = Sql; 91 for (int i = 0; i < parameter.Length; i++) 92 { 93 cmd.Parameters.Add(parameter[i]); 94 } 95 return cmd.ExecuteScalar(); 96 } 97 } 98 /// <summary> 99 /// 在数据库中,进行数据库的查询操作 100 /// </summary> 101 /// <param name="Sql"></param> 102 /// <param name="parameter"></param> 103 /// <returns></returns> 104 public static IDataReader ExecuteReader(string Sql, params IDbDataParameter[] parameter) 105 { 106 using (IDbConnection conn = getConnection()) 107 using (IDbCommand cmd = conn.CreateCommand()) 108 { 109 conn.Open(); 110 cmd.CommandText = Sql; 111 for (int i = 0; i < parameter.Length; i++) 112 { 113 cmd.Parameters.Add(parameter[i]); 114 } 115 return cmd.ExecuteReader(CommandBehavior.CloseConnection); 116 } 117 } 118 } 119 /// <summary> 120 /// 判断是什么数据库 121 /// </summary> 122 public class DBdatabase 123 { 124 public static DatabaseList database { get; set; } 125 } 126 public class AdoHelper 127 { 128 /// <summary> 129 /// 将字符串转换成枚举类型 130 /// </summary> 131 /// <typeparam name="T">枚举类型</typeparam> 132 /// <param name="value">字符串值</param> 133 /// <returns></returns> 134 public static T EnumParse<T>(string value) 135 { 136 return (T)Enum.Parse(typeof(T), value); 137 } 138 139 /// <summary> 140 /// 对数据库进行操作 141 /// </summary> 142 /// <param name="conn">获取一个连接</param> 143 /// <returns></returns> 144 public static IDbCommand CreateCommand(IDbConnection conn) 145 { 146 IDbCommand cmd = conn.CreateCommand(); 147 conn.Open(); 148 return cmd; 149 } 150 } 151 /// <summary> 152 /// 数据库类型 153 /// </summary> 154 public enum DatabaseList 155 { 156 MYSQL = 0, 157 SQLSERVER = 1 158 }