常用操作Oracle数据库的类
public const string CONN_STRING = "user id=用户名;data source=数据源;password=密码";
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;
cmd.CommandType = cmdType;
if (trans != null)
cmd.Transaction = trans;
if (cmdParms != null)
{
foreach (OracleParameter parm in cmdParms)
cmd.Parameters.Add(parm);
}
}
public static int ExecuteNonQuery(string connString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
using (OracleConnection conn = new OracleConnection(connString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
int val = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return val;
}
}
public static object ExecuteScalar(string connString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
using (OracleConnection conn = new OracleConnection(connString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
object val = cmd.ExecuteScalar();
cmd.Parameters.Clear();
return val;
}
}
public static OracleDataReader ExecuteReader(string connString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
OracleConnection conn = new OracleConnection(connString);
try
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
return rdr;
}
catch (Exception e)
{
conn.Close();
throw e;
}
}
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 = 9;
try
{
val = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
string ss = ex.ToString();
throw ex;
}
cmd.Parameters.Clear();
return val;
}
public static DataSet Adapter(string connString, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
DataSet ds = new DataSet();
using (OracleConnection conn = new OracleConnection(connString))
{
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataAdapter ada = new OracleDataAdapter(cmd);
ada.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
}
public static DataSet Adapter( OracleTransaction ts, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
cmd.Transaction = ts;
DataSet ds = new DataSet();
PrepareCommand(cmd, ts.Connection, null, cmdType, cmdText, cmdParms);
OracleDataAdapter ada = new OracleDataAdapter(cmd);
ada.Fill(ds);
cmd.Parameters.Clear();
return ds;
}
public static DataSet Adapter(OracleConnection conn, CommandType cmdType, string cmdText, params OracleParameter[] cmdParms)
{
OracleCommand cmd = new OracleCommand();
DataSet ds = new DataSet();
PrepareCommand(cmd, conn, null, cmdType, cmdText, cmdParms);
OracleDataAdapter ada = new OracleDataAdapter(cmd);
ada.Fill(ds);
cmd.Parameters.Clear();
return ds;
}