暂时只有三个Helper,分别是MySqlHelper/OracleHelper/SQLHelper(MSSqlServer).
暂时只有三个Helper,分别是MySqlHelper/OracleHelper/SQLHelper(MSSqlServer).MySQLHelper是取自mysql-connector-net-5.0.8.1的(dev.mysql.com有下载),其余两个取自.NET Pet Shop 4.0,简单将它们放到一个命名空间内了.这里下载
1

namespace Alacky.DBUtility
{2

3

/**//// <summary>4
/// The SqlHelper class is intended to encapsulate high performance, 5
/// scalable best practices for common uses of SqlClient.6
/// </summary>7

public abstract class SqlHelper
{ 8
9
// Hashtable to store cached parameters10
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());11

12

/**//// <summary>13
/// Execute a SqlCommand (that returns no resultset) against the database specified in the connection string 14
/// using the provided parameters.15
/// </summary>16
/// <remarks>17
/// e.g.: 18
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));19
/// </remarks>20
/// <param name="connectionString">a valid connection string for a SqlConnection</param>21
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>22
/// <param name="commandText">the stored procedure name or T-SQL command</param>23
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>24
/// <returns>an int representing the number of rows affected by the command</returns>25

public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{26

27
SqlCommand cmd = new SqlCommand();28

29

using (SqlConnection conn = new SqlConnection(connectionString))
{30
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);31
int val = cmd.ExecuteNonQuery();32
cmd.Parameters.Clear();33
return val;34
}35
}36

37

/**//// <summary>38
/// Execute a SqlCommand (that returns no resultset) against an existing database connection 39
/// using the provided parameters.40
/// </summary>41
/// <remarks>42
/// e.g.: 43
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));44
/// </remarks>45
/// <param name="conn">an existing database connection</param>46
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>47
/// <param name="commandText">the stored procedure name or T-SQL command</param>48
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>49
/// <returns>an int representing the number of rows affected by the command</returns>50

public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{51

52
SqlCommand cmd = new SqlCommand();53

54
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);55
int val = cmd.ExecuteNonQuery();56
cmd.Parameters.Clear();57
return val;58
}59

60

/**//// <summary>61
/// Execute a SqlCommand (that returns no resultset) using an existing SQL Transaction 62
/// using the provided parameters.63
/// </summary>64
/// <remarks>65
/// e.g.: 66
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));67
/// </remarks>68
/// <param name="trans">an existing sql transaction</param>69
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>70
/// <param name="commandText">the stored procedure name or T-SQL command</param>71
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>72
/// <returns>an int representing the number of rows affected by the command</returns>73

public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{74
SqlCommand cmd = new SqlCommand();75
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);76
int val = cmd.ExecuteNonQuery();77
cmd.Parameters.Clear();78
return val;79
}80

81

/**//// <summary>82
/// Execute a SqlCommand that returns a resultset against the database specified in the connection string 83
/// using the provided parameters.84
/// </summary>85
/// <remarks>86
/// e.g.: 87
/// SqlDataReader r = ExecuteReader(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));88
/// </remarks>89
/// <param name="connectionString">a valid connection string for a SqlConnection</param>90
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>91
/// <param name="commandText">the stored procedure name or T-SQL command</param>92
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>93
/// <returns>A SqlDataReader containing the results</returns>94

public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{95
SqlCommand cmd = new SqlCommand();96
SqlConnection conn = new SqlConnection(connectionString);97

98
// we use a try/catch here because if the method throws an exception we want to 99
// close the connection throw code, because no datareader will exist, hence the 100
// commandBehaviour.CloseConnection will not work101

try
{102
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);103
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);104
cmd.Parameters.Clear();105
return rdr;106
}107

catch
{108
conn.Close();109
throw;110
}111
}112

113

/**//// <summary>114
/// Execute a SqlCommand that returns the first column of the first record against the database specified in the connection string 115
/// using the provided parameters.116
/// </summary>117
/// <remarks>118
/// e.g.: 119
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));120
/// </remarks>121
/// <param name="connectionString">a valid connection string for a SqlConnection</param>122
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>123
/// <param name="commandText">the stored procedure name or T-SQL command</param>124
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>125
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>126

public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{127
SqlCommand cmd = new SqlCommand();128

129

using (SqlConnection connection = new SqlConnection(connectionString))
{130
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);131
object val = cmd.ExecuteScalar();132
cmd.Parameters.Clear();133
return val;134
}135
}136

137

/**//// <summary>138
/// Execute a SqlCommand that returns the first column of the first record against an existing database connection 139
/// using the provided parameters.140
/// </summary>141
/// <remarks>142
/// e.g.: 143
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new SqlParameter("@prodid", 24));144
/// </remarks>145
/// <param name="conn">an existing database connection</param>146
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>147
/// <param name="commandText">the stored procedure name or T-SQL command</param>148
/// <param name="commandParameters">an array of SqlParamters used to execute the command</param>149
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>150

public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
{151

152
SqlCommand cmd = new SqlCommand();153

154
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);155
object val = cmd.ExecuteScalar();156
cmd.Parameters.Clear();157
return val;158
}159

160

/**//// <summary>161
/// add parameter array to the cache162
/// </summary>163
/// <param name="cacheKey">Key to the parameter cache</param>164
/// <param name="cmdParms">an array of SqlParamters to be cached</param>165

public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
{166
parmCache[cacheKey] = commandParameters;167
}168

169

/**//// <summary>170
/// Retrieve cached parameters171
/// </summary>172
/// <param name="cacheKey">key used to lookup parameters</param>173
/// <returns>Cached SqlParamters array</returns>174

public static SqlParameter[] GetCachedParameters(string cacheKey)
{175
SqlParameter[] cachedParms = (SqlParameter[])parmCache[cacheKey];176

177
if (cachedParms == null)178
return null;179

180
SqlParameter[] clonedParms = new SqlParameter[cachedParms.Length];181

182
for (int i = 0, j = cachedParms.Length; i < j; i++)183
clonedParms[i] = (SqlParameter)((ICloneable)cachedParms[i]).Clone();184

185
return clonedParms;186
}187

188

/**//// <summary>189
/// Prepare a command for execution190
/// </summary>191
/// <param name="cmd">SqlCommand object</param>192
/// <param name="conn">SqlConnection object</param>193
/// <param name="trans">SqlTransaction object</param>194
/// <param name="cmdType">Cmd type e.g. stored procedure or text</param>195
/// <param name="cmdText">Command text, e.g. Select * from Products</param>196
/// <param name="cmdParms">SqlParameters to use in the command</param>197

private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
{198

199
if (conn.State != ConnectionState.Open)200
conn.Open();201

202
cmd.Connection = conn;203
cmd.CommandText = cmdText;204

205
if (trans != null)206
cmd.Transaction = trans;207

208
cmd.CommandType = cmdType;209

210

if (cmdParms != null)
{211
foreach (SqlParameter parm in cmdParms)212
cmd.Parameters.Add(parm);213
}214
}215
}216
}1
using System;2
using System.Configuration;3
using System.Data;4
using System.Data.OracleClient;5
using System.Collections;6

7

namespace Alacky.DBUtility
{8

9

/**//// <summary>10
/// A helper class used to execute queries against an Oracle database11
/// </summary>12

public abstract class OracleHelper
{13

14
//Create a hashtable for the parameter cached15
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());16

17

/**//// <summary>18
/// Execute a database query which does not include a select19
/// </summary>20
/// <param name="connString">Connection string to database</param>21
/// <param name="cmdType">Command type either stored procedure or SQL</param>22
/// <param name="cmdText">Acutall SQL Command</param>23
/// <param name="commandParameters">Parameters to bind to the command</param>24
/// <returns></returns>25

public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{26
// Create a new Oracle command27
OracleCommand cmd = new OracleCommand();28

29
//Create a connection30

using (OracleConnection connection = new OracleConnection(connectionString))
{31

32
//Prepare the command33
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);34

35
//Execute the command36
int val = cmd.ExecuteNonQuery();37
cmd.Parameters.Clear();38
return val;39
}40
}41

42

/**//// <summary>43
/// Execute an OracleCommand (that returns no resultset) against an existing database transaction 44
/// using the provided parameters.45
/// </summary>46
/// <remarks>47
/// e.g.: 48
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));49
/// </remarks>50
/// <param name="trans">an existing database transaction</param>51
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>52
/// <param name="commandText">the stored procedure name or PL/SQL command</param>53
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>54
/// <returns>an int representing the number of rows affected by the command</returns>55

public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{56
OracleCommand cmd = new OracleCommand();57
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters);58
int val = cmd.ExecuteNonQuery();59
cmd.Parameters.Clear();60
return val;61
}62

63

/**//// <summary>64
/// Execute an OracleCommand (that returns no resultset) against an existing database connection 65
/// using the provided parameters.66
/// </summary>67
/// <remarks>68
/// e.g.: 69
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));70
/// </remarks>71
/// <param name="conn">an existing database connection</param>72
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>73
/// <param name="commandText">the stored procedure name or PL/SQL command</param>74
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>75
/// <returns>an int representing the number of rows affected by the command</returns>76

public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{77

78
OracleCommand cmd = new OracleCommand();79

80
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);81
int val = cmd.ExecuteNonQuery();82
cmd.Parameters.Clear();83
return val;84
}85

86

/**//// <summary>87
/// Execute a select query that will return a result set88
/// </summary>89
/// <param name="connString">Connection string</param>90
//// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>91
/// <param name="commandText">the stored procedure name or PL/SQL command</param>92
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>93
/// <returns></returns>94

public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{95

96
//Create the command and connection97
OracleCommand cmd = new OracleCommand();98
OracleConnection conn = new OracleConnection(connectionString);99

100

try
{101
//Prepare the command to execute102
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);103

104
//Execute the query, stating that the connection should close when the resulting datareader has been read105
OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);106
cmd.Parameters.Clear();107
return rdr;108

109
}110

catch
{111

112
//If an error occurs close the connection as the reader will not be used and we expect it to close the connection113
conn.Close();114
throw;115
}116
}117

118

/**//// <summary>119
/// Execute an OracleCommand that returns the first column of the first record against the database specified in the connection string 120
/// using the provided parameters.121
/// </summary>122
/// <remarks>123
/// e.g.: 124
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));125
/// </remarks>126
/// <param name="connectionString">a valid connection string for a SqlConnection</param>127
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>128
/// <param name="commandText">the stored procedure name or PL/SQL command</param>129
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>130
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>131

public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{132
OracleCommand cmd = new OracleCommand();133

134

using (OracleConnection conn = new OracleConnection(connectionString))
{135
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters);136
object val = cmd.ExecuteScalar();137
cmd.Parameters.Clear();138
return val;139
}140
}141

142

/**//// <summary>143
/// Execute a OracleCommand (that returns a 1x1 resultset) against the specified SqlTransaction144
/// using the provided parameters.145
/// </summary>146
/// <param name="transaction">A valid SqlTransaction</param>147
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param>148
/// <param name="commandText">The stored procedure name or PL/SQL command</param>149
/// <param name="commandParameters">An array of OracleParamters used to execute the command</param>150
/// <returns>An object containing the value in the 1x1 resultset generated by the command</returns>151

public static object ExecuteScalar(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters)
{152
if(transaction == null)153
throw new ArgumentNullException("transaction");154
if(transaction != null && transaction.Connection == null)155
throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction");156

157
// Create a command and prepare it for execution158
OracleCommand cmd = new OracleCommand();159

160
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters);161

162
// Execute the command & return the results163
object retval = cmd.ExecuteScalar();164

165
// Detach the SqlParameters from the command object, so they can be used again166
cmd.Parameters.Clear();167
return retval;168
}169

170

/**//// <summary>171
/// Execute an OracleCommand that returns the first column of the first record against an existing database connection 172
/// using the provided parameters.173
/// </summary>174
/// <remarks>175
/// e.g.: 176
/// Object obj = ExecuteScalar(conn, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24));177
/// </remarks>178
/// <param name="conn">an existing database connection</param>179
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param>180
/// <param name="commandText">the stored procedure name or PL/SQL command</param>181
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param>182
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns>183

public static object ExecuteScalar(OracleConnection connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters)
{184
OracleCommand cmd = new OracleCommand();185

186
PrepareCommand(cmd, connectionString, null, cmdType, cmdText, commandParameters);187
object val = cmd.ExecuteScalar();188
cmd.Parameters.Clear();189
return val;190
}191

192

/**//// <summary>193
/// Add a set of parameters to the cached194
/// </summary>195
/// <param name="cacheKey">Key value to look up the parameters</param>196
/// <param name="commandParameters">Actual parameters to cached</param>197

public static void CacheParameters(string cacheKey, params OracleParameter[] commandParameters)
{198
parmCache[cacheKey] = commandParameters;199
}200

201

/**//// <summary>202
/// Fetch parameters from the cache203
/// </summary>204
/// <param name="cacheKey">Key to look up the parameters</param>205
/// <returns></returns>206

public static OracleParameter[] GetCachedParameters(string cacheKey)
{207
OracleParameter[] cachedParms = (OracleParameter[])parmCache[cacheKey];208

209
if (cachedParms == null)210
return null;211

212
// If the parameters are in the cache213
OracleParameter[] clonedParms = new OracleParameter[cachedParms.Length];214

215
// return a copy of the parameters216
for (int i = 0, j = cachedParms.Length; i < j; i++)217
clonedParms[i] = (OracleParameter)((ICloneable)cachedParms[i]).Clone();218

219
return clonedParms;220
}221

222

/**//// <summary>223
/// Internal function to prepare a command for execution by the database224
/// </summary>225
/// <param name="cmd">Existing command object</param>226
/// <param name="conn">Database connection object</param>227
/// <param name="trans">Optional transaction object</param>228
/// <param name="cmdType">Command type, e.g. stored procedure</param>229
/// <param name="cmdText">Command test</param>230
/// <param name="commandParameters">Parameters for the command</param>231

private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters)
{232

233
//Open the connection if required234
if (conn.State != ConnectionState.Open)235
conn.Open();236

237
//Set up the command238
cmd.Connection = conn;239
cmd.CommandText = cmdText;240
cmd.CommandType = cmdType;241

242
//Bind it to the transaction if it exists243
if (trans != null)244
cmd.Transaction = trans;245

246
// Bind the parameters passed in247

if (commandParameters != null)
{248
foreach (OracleParameter parm in commandParameters)249
cmd.Parameters.Add(parm);250
}251
}252

253

/**//// <summary>254
/// Converter to use boolean data type with Oracle255
/// </summary>256
/// <param name="value">Value to convert</param>257
/// <returns></returns>258

public static string OraBit(bool value)
{259
if(value)260
return "Y";261
else262
return "N";263
}264

265

/**//// <summary>266
/// Converter to use boolean data type with Oracle267
/// </summary>268
/// <param name="value">Value to convert</param>269
/// <returns></returns>270

public static bool OraBool(string value)
{271
if(value.Equals("Y"))272
return true;273
else274
return false;275
} 276
}277
}278

1
using System;2
using System.Configuration;3
using System.Data;4
using MySql.Data.MySqlClient;5
using MySql.Data.Types;6
using System.Collections;7

8
namespace Alacky.DBUtility9


{10
public abstract class MySQLHelper 11

{12
public static DataRow ExecuteDataRow(string connectionString, string commandText, params MySqlParameter[] parms)13

{14
return MySql.Data.MySqlClient.MySqlHelper.ExecuteDataRow(connectionString, commandText, parms);15
}16
public static DataSet ExecuteDataset(MySqlConnection connection, string commandText)17

{18
return MySql.Data.MySqlClient.MySqlHelper.ExecuteDataset(connection, commandText);19
}20
public static DataSet ExecuteDataset(string connectionString, string commandText)21

{22
return MySql.Data.MySqlClient.MySqlHelper.ExecuteDataset(connectionString, commandText);23
}24
public static DataSet ExecuteDataset(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)25

{26
return MySql.Data.MySqlClient.MySqlHelper.ExecuteDataset(connection, commandText, commandParameters);27
}28
public static DataSet ExecuteDataset(string connectionString, string commandText, params MySqlParameter[] commandParameters)29

{30
return MySql.Data.MySqlClient.MySqlHelper.ExecuteDataset(connectionString, commandText, commandParameters);31
}32
public static int ExecuteNonQuery(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)33

{34
return MySql.Data.MySqlClient.MySqlHelper.ExecuteNonQuery(connection, commandText, commandParameters);35
}36
public static int ExecuteNonQuery(string connectionString, string commandText, params MySqlParameter[] parms)37

{38
return MySql.Data.MySqlClient.MySqlHelper.ExecuteNonQuery(connectionString, commandText, parms);39
}40
public static MySqlDataReader ExecuteReader(string connectionString, string commandText)41

{42
return MySql.Data.MySqlClient.MySqlHelper.ExecuteReader(connectionString, commandText);43
}44
public static MySqlDataReader ExecuteReader(string connectionString, string commandText, params MySqlParameter[] commandParameters)45

{46
return MySql.Data.MySqlClient.MySqlHelper.ExecuteReader(connectionString, commandText, commandParameters);47
}48
public static object ExecuteScalar(MySqlConnection connection, string commandText)49

{50
return MySql.Data.MySqlClient.MySqlHelper.ExecuteScalar(connection, commandText);51
}52
public static object ExecuteScalar(string connectionString, string commandText)53

{54
return MySql.Data.MySqlClient.MySqlHelper.ExecuteScalar(connectionString, commandText);55
}56
public static object ExecuteScalar(MySqlConnection connection, string commandText, params MySqlParameter[] commandParameters)57

{58
return MySql.Data.MySqlClient.MySqlHelper.ExecuteScalar(connection, commandText, commandParameters);59
}60
public static object ExecuteScalar(string connectionString, string commandText, params MySqlParameter[] commandParameters)61

{62
return MySql.Data.MySqlClient.MySqlHelper.ExecuteScalar(connectionString, commandText, commandParameters);63
}64
public static void UpdateDataSet(string connectionString, string commandText, DataSet ds, string tablename)65

{66
MySql.Data.MySqlClient.MySqlHelper.UpdateDataSet(connectionString, commandText, ds, tablename);67
}68
}69
}70

想做些令人惊奇的东西出来
posted on
浙公网安备 33010602011771号