温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:产品管理系统(三层结构示例项目源码)
当前文件:
OVHKLMBIPZ872/DBUtility/OracleHelper.cs[14K,2009-6-12 11:51:42],打开代码结构图
OVHKLMBIPZ872/DBUtility/OracleHelper.cs[14K,2009-6-12 11:51:42],打开代码结构图1using System; 2
using System.Configuration; 3
using System.Data; 4
using System.Data.OracleClient; 5
using System.Collections; 6
7
namespace LiTianPing.DBUtility 8
{ 9
10
/// <summary> 11
/// A helper class used to execute queries against an Oracle database 12
/// </summary> 13
public abstract class OracleHelper 14
{ 15
16
// Read the connection strings from the configuration file 17
public static readonly string ConnectionStringLocalTransaction = ConfigurationManager.AppSettings["OraConnString1"]; 18
public static readonly string ConnectionStringInventoryDistributedTransaction = ConfigurationManager.AppSettings["OraConnString2"]; 19
public static readonly string ConnectionStringOrderDistributedTransaction = ConfigurationManager.AppSettings["OraConnString3"]; 20
public static readonly string ConnectionStringProfile = ConfigurationManager.AppSettings["OraProfileConnString"]; 21
public static readonly string ConnectionStringMembership = ConfigurationManager.AppSettings["OraMembershipConnString"]; 22
23
//Create a hashtable for the parameter cached 24
private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable()); 25
26
/// <summary> 27
/// Execute a database query which does not include a select 28
/// </summary> 29
/// <param name="connString">Connection string to database</param> 30
/// <param name="cmdType">Command type either stored procedure or SQL</param> 31
/// <param name="cmdText">Acutall SQL Command</param> 32
/// <param name="commandParameters">Parameters to bind to the command</param> 33
/// <returns></returns> 34
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters) 35
{ 36
// Create a new Oracle command 37
OracleCommand cmd = new OracleCommand(); 38
39
//Create a connection 40
using (OracleConnection connection = new OracleConnection(connectionString)) 41
{ 42
43
//Prepare the command 44
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); 45
46
//Execute the command 47
int val = cmd.ExecuteNonQuery(); 48
cmd.Parameters.Clear(); 49
return val; 50
} 51
} 52
53
/// <summary> 54
/// Execute an OracleCommand (that returns no resultset) against an existing database transaction 55
/// using the provided parameters. 56
/// </summary> 57
/// <remarks> 58
/// e.g.: 59
/// int result = ExecuteNonQuery(trans, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24)); 60
/// </remarks> 61
/// <param name="trans">an existing database transaction</param> 62
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> 63
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 64
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param> 65
/// <returns>an int representing the number of rows affected by the command</returns> 66
public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters) 67
{ 68
OracleCommand cmd = new OracleCommand(); 69
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, commandParameters); 70
int val = cmd.ExecuteNonQuery(); 71
cmd.Parameters.Clear(); 72
return val; 73
} 74
75
/// <summary> 76
/// Execute an OracleCommand (that returns no resultset) against an existing database connection 77
/// using the provided parameters. 78
/// </summary> 79
/// <remarks> 80
/// e.g.: 81
/// int result = ExecuteNonQuery(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24)); 82
/// </remarks> 83
/// <param name="conn">an existing database connection</param> 84
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> 85
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 86
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param> 87
/// <returns>an int representing the number of rows affected by the command</returns> 88
public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters) 89
{ 90
91
OracleCommand cmd = new OracleCommand(); 92
93
PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters); 94
int val = cmd.ExecuteNonQuery(); 95
cmd.Parameters.Clear(); 96
return val; 97
} 98
99
/// <summary> 100
/// Execute a select query that will return a result set 101
/// </summary> 102
/// <param name="connString">Connection string</param> 103
//// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> 104
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 105
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param> 106
/// <returns></returns> 107
public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters) 108
{ 109
110
//Create the command and connection 111
OracleCommand cmd = new OracleCommand(); 112
OracleConnection conn = new OracleConnection(connectionString); 113
114
try 115
{ 116
//Prepare the command to execute 117
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); 118
119
//Execute the query, stating that the connection should close when the resulting datareader has been read 120
OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 121
cmd.Parameters.Clear(); 122
return rdr; 123
124
} 125
catch 126
{ 127
128
//If an error occurs close the connection as the reader will not be used and we expect it to close the connection 129
conn.Close(); 130
throw; 131
} 132
} 133
134
/// <summary> 135
/// Execute an OracleCommand that returns the first column of the first record against the database specified in the connection string 136
/// using the provided parameters. 137
/// </summary> 138
/// <remarks> 139
/// e.g.: 140
/// Object obj = ExecuteScalar(connString, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24)); 141
/// </remarks> 142
/// <param name="connectionString">a valid connection string for a SqlConnection</param> 143
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> 144
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 145
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param> 146
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns> 147
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters) 148
{ 149
OracleCommand cmd = new OracleCommand(); 150
151
using (OracleConnection conn = new OracleConnection(connectionString)) 152
{ 153
PrepareCommand(cmd, conn, null, cmdType, cmdText, commandParameters); 154
object val = cmd.ExecuteScalar(); 155
cmd.Parameters.Clear(); 156
return val; 157
} 158
} 159
160
/// <summary> 161
/// Execute a OracleCommand (that returns a 1x1 resultset) against the specified SqlTransaction 162
/// using the provided parameters. 163
/// </summary> 164
/// <param name="transaction">A valid SqlTransaction</param> 165
/// <param name="commandType">The CommandType (stored procedure, text, etc.)</param> 166
/// <param name="commandText">The stored procedure name or PL/SQL command</param> 167
/// <param name="commandParameters">An array of OracleParamters used to execute the command</param> 168
/// <returns>An object containing the value in the 1x1 resultset generated by the command</returns> 169
public static object ExecuteScalar(OracleTransaction transaction, CommandType commandType, string commandText, params OracleParameter[] commandParameters) 170
{ 171
if (transaction == null) 172
throw new ArgumentNullException("transaction"); 173
if (transaction != null && transaction.Connection == null) 174
throw new ArgumentException("The transaction was rollbacked or commited, please provide an open transaction.", "transaction"); 175
176
// Create a command and prepare it for execution 177
OracleCommand cmd = new OracleCommand(); 178
179
PrepareCommand(cmd, transaction.Connection, transaction, commandType, commandText, commandParameters); 180
181
// Execute the command & return the results 182
object retval = cmd.ExecuteScalar(); 183
184
// Detach the SqlParameters from the command object, so they can be used again 185
cmd.Parameters.Clear(); 186
return retval; 187
} 188
189
/// <summary> 190
/// Execute an OracleCommand that returns the first column of the first record against an existing database connection 191
/// using the provided parameters. 192
/// </summary> 193
/// <remarks> 194
/// e.g.: 195
/// Object obj = ExecuteScalar(conn, CommandType.StoredProcedure, "PublishOrders", new OracleParameter(":prodid", 24)); 196
/// </remarks> 197
/// <param name="conn">an existing database connection</param> 198
/// <param name="commandType">the CommandType (stored procedure, text, etc.)</param> 199
/// <param name="commandText">the stored procedure name or PL/SQL command</param> 200
/// <param name="commandParameters">an array of OracleParamters used to execute the command</param> 201
/// <returns>An object that should be converted to the expected type using Convert.To{Type}</returns> 202
public static object ExecuteScalar(OracleConnection connectionString, CommandType cmdType, string cmdText, params OracleParameter[] commandParameters) 203
{ 204
OracleCommand cmd = new OracleCommand(); 205
206
PrepareCommand(cmd, connectionString, null, cmdType, cmdText, commandParameters); 207
object val = cmd.ExecuteScalar(); 208
cmd.Parameters.Clear(); 209
return val; 210
} 211
212
/// <summary> 213
/// Add a set of parameters to the cached 214
/// </summary> 215
/// <param name="cacheKey">Key value to look up the parameters</param> 216
/// <param name="commandParameters">Actual parameters to cached</param> 217
public static void CacheParameters(string cacheKey, params OracleParameter[] commandParameters) 218
{ 219
parmCache[cacheKey] = commandParameters; 220
} 221
222
/// <summary> 223
/// Fetch parameters from the cache 224
/// </summary> 225
/// <param name="cacheKey">Key to look up the parameters</param> 226
/// <returns></returns> 227
public static OracleParameter[] GetCachedParameters(string cacheKey) 228
{ 229
OracleParameter[] cachedParms = (OracleParameter[])parmCache[cacheKey]; 230
231
if (cachedParms == null) 232
return null; 233
234
// If the parameters are in the cache 235
OracleParameter[] clonedParms = new OracleParameter[cachedParms.Length]; 236
237
// return a copy of the parameters 238
for (int i = 0, j = cachedParms.Length; i < j; i++) 239
clonedParms[i] = (OracleParameter)((ICloneable)cachedParms[i]).Clone(); 240
241
return clonedParms; 242
} 243
244
/// <summary> 245
/// Internal function to prepare a command for execution by the database 246
/// </summary> 247
/// <param name="cmd">Existing command object</param> 248
/// <param name="conn">Database connection object</param> 249
/// <param name="trans">Optional transaction object</param> 250
/// <param name="cmdType">Command type, e.g. stored procedure</param> 251
/// <param name="cmdText">Command test</param> 252
/// <param name="commandParameters">Parameters for the command</param> 253
private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] commandParameters) 254
{ 255
256
//Open the connection if required 257
if (conn.State != ConnectionState.Open) 258
conn.Open(); 259
260
//Set up the command 261
cmd.Connection = conn; 262
cmd.CommandText = cmdText; 263
cmd.CommandType = cmdType; 264
265
//Bind it to the transaction if it exists 266
if (trans != null) 267
cmd.Transaction = trans; 268
269
// Bind the parameters passed in 270
if (commandParameters != null) 271
{ 272
foreach (OracleParameter parm in commandParameters) 273
cmd.Parameters.Add(parm); 274
} 275
} 276
277
/// <summary> 278
/// Converter to use boolean data type with Oracle 279
/// </summary> 280
/// <param name="value">Value to convert</param> 281
/// <returns></returns> 282
public static string OraBit(bool value) 283
{ 284
if (value) 285
return "Y"; 286
else 287
return "N"; 288
} 289
290
/// <summary> 291
/// Converter to use boolean data type with Oracle 292
/// </summary> 293
/// <param name="value">Value to convert</param> 294
/// <returns></returns> 295
public static bool OraBool(string value) 296
{ 297
if (value.Equals("Y")) 298
return true; 299
else 300
return false; 301
} 302
} 303
} 304






}