原文地址:http://www.cnblogs.com/haibing0107/p/6143922.html
using System;
using System.Data;
using System.Collections.Generic;
using System.Configuration;
using System.Data.OracleClient;
using System.Text;
using System.IO;
/// <summary>
/// Oracle數據庫操作類
/// </summary>
public static class OracleHelper
{
/// <summary>
/// 執行數據庫非查詢操作,返回受影響的行數
/// </summary>
/// <param name="connectionString">數據庫連接字符串</param>
/// <param name="cmdType">命令的類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前查詢操作影響的數據行數</returns>
public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
using (OracleConnection conn = new OracleConnection(connectionString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
/// <summary>
/// 執行數據庫事務非查詢操作,返回受影響的行數
/// </summary>
/// <param name="transaction">數據庫事務對象</param>
/// <param name="cmdType">Command類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前事務查詢操作影響的數據行數</returns>
public static int ExecuteNonQuery(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
PrepareCommand(cmd, trans.Connection, trans, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 執行數據庫非查詢操作,返回受影響的行數
/// </summary>
/// <param name="connection">Oracle數據庫連接對象</param>
/// <param name="cmdType">Command類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前查詢操作影響的數據行數</returns>
public static int ExecuteNonQuery(OracleConnection connection, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
if (connection == null)
throw new ArgumentNullException("當前數據庫連接不存在");
OracleCommand cmd = new OracleCommand();
PrepareCommand(cmd, connection, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
/// <summary>
/// 執行數據庫查詢操作,返回OracleDataReader類型的內存結果集
/// </summary>
/// <param name="connectionString">數據庫連接字符串</param>
/// <param name="cmdType">命令的類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前查詢操作返回的OracleDataReader類型的內存結果集</returns>
public static OracleDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return reader;
}
catch
{
cmd.Dispose();
conn.Close();
throw;
}
}
/// <summary>
/// 執行數據庫查詢操作,返回DataSet類型的結果集
/// </summary>
/// <param name="connectionString">數據庫連接字符串</param>
/// <param name="cmdType">命令的類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前查詢操作返回的DataSet類型的結果集</returns>
public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
DataSet ds = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataAdapter adapter = new OracleDataAdapter();
adapter.SelectCommand = cmd;
ds = new DataSet();
adapter.Fill(ds);
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return ds;
}
/// <summary>
/// 執行數據庫查詢操作,返回DataTable類型的結果集
/// </summary>
/// <param name="connectionString">數據庫連接字符串</param>
/// <param name="cmdType">命令的類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前查詢操作返回的DataTable類型的結果集</returns>
public static DataTable ExecuteDataTable(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
DataTable dt = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataAdapter adapter = new OracleDataAdapter();
adapter.SelectCommand = cmd;
dt = new DataTable();
adapter.Fill(dt);
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return dt;
}
/// <summary>
/// 執行數據庫查詢操作,返回結果集中位於第一行第一列的Object類型的值
/// </summary>
/// <param name="connectionString">數據庫連接字符串</param>
/// <param name="cmdType">命令的類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前查詢操作返回的結果集中位於第一行第一列的Object類型的值</returns>
public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connectionString);
object result = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 執行數據庫事務查詢操作,返回結果集中位於第一行第一列的Object類型的值
/// </summary>
/// <param name="trans">一個已存在的數據庫事務對象</param>
/// <param name="commandType">命令類型</param>
/// <param name="commandText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前事務查詢操作返回的結果集中位於第一行第一列的Object類型的值</returns>
public static object ExecuteScalar(OracleTransaction trans, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
if (trans == null)
throw new ArgumentNullException("當前數據庫事務不存在");
OracleConnection conn = trans.Connection;
if (conn == null)
throw new ArgumentException("當前事務所在的數據庫連接不存在");
OracleCommand cmd = new OracleCommand();
object result = null;
try
{
PrepareCommand(cmd, conn, trans, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
trans.Dispose();
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 執行數據庫查詢操作,返回結果集中位於第一行第一列的Object類型的值
/// </summary>
/// <param name="conn">數據庫連接對象</param>
/// <param name="cmdType">Command類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
/// <returns>當前查詢操作返回的結果集中位於第一行第一列的Object類型的值</returns>
public static object ExecuteScalar(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
if (conn == null) throw new ArgumentException("當前數據庫連接不存在");
OracleCommand cmd = new OracleCommand();
object result = null;
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
result = cmd.ExecuteScalar();
cmd.Parameters.Clear();
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
conn.Dispose();
}
return result;
}
/// <summary>
/// 執行數據庫命令前的准備工作
/// </summary>
/// <param name="cmd">Command對象</param>
/// <param name="conn">數據庫連接對象</param>
/// <param name="trans">事務對象</param>
/// <param name="cmdType">Command類型</param>
/// <param name="cmdText">Oracle存儲過程名稱或PL/SQL命令</param>
/// <param name="cmdParms">命令參數集合</param>
private static void PrepareCommand(OracleCommand cmd, OracleConnection conn, OracleTransaction trans, CommandType cmdType, string cmdText, OracleParameter[] cmdParms)
{
if (conn.State != ConnectionState.Open)
conn.Open();
cmd.Connection = conn;
cmd.CommandText = cmdText;
if (trans != null)
cmd.Transaction = trans;
cmd.CommandType = cmdType;
if (cmdParms != null)
{
foreach (OracleParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
/// <summary>
/// 將.NET日期時間類型轉化為Oracle兼容的日期時間格式字符串
/// </summary>
/// <param name="date">.NET日期時間類型對象</param>
/// <returns>Oracle兼容的日期時間格式字符串(如該字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns>
public static string GetOracleDateFormat(DateTime date)
{
return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','YYYY-MM-DD')";
}
/// <summary>
/// 將.NET日期時間類型轉化為Oracle兼容的日期格式字符串
/// </summary>
/// <param name="date">.NET日期時間類型對象</param>
/// <param name="format">Oracle日期時間類型格式化限定符</param>
/// <returns>Oracle兼容的日期時間格式字符串(如該字符串:TO_DATE('2007-12-1','YYYY-MM-DD'))</returns>
public static string GetOracleDateFormat(DateTime date, string format)
{
if (format == null || format.Trim() == "") format = "YYYY-MM-DD";
return "TO_DATE('" + date.ToString("yyyy-M-dd") + "','" + format + "')";
}
/// <summary>
/// 將指定的關鍵字處理為模糊查詢時的合法參數值
/// </summary>
/// <param name="source">待處理的查詢關鍵字</param>
/// <returns>過濾后的查詢關鍵字</returns>
public static string HandleLikeKey(string source)
{
if (source == null || source.Trim() == "") return null;
source = source.Replace("[", "[]]");
source = source.Replace("_", "[_]");
source = source.Replace("%", "[%]");
return ("%" + source + "%");
}
}
