C#操作ORACL数据库调用ORACL存储过程
|
admin
2025年7月11日 14:41
本文热度 1445
|
一、执行存储过程
public static Boolean SqlProcedure(string SQLString) { using (OracleConnection connection = new OracleConnection(connectionString)) { try { connection.Open(); OracleCommand command = new OracleCommand(SQLString, connection); int recordCount = command.ExecuteNonQuery();
return true; } catch (Exception ex) { LogHelper.WriteErrorLog("SQL:" + SQLString, ex); Global.strMessage = ex.Message.ToString(); return false; throw new Exception(ex.Message); } finally { connection.Close(); } } }
二、一次执行多个存储过程,带事务回滚
public static Boolean SqlProcedure(ArrayList SQLArrayList) { using (OracleConnection connection = new OracleConnection(connectionString)) { connection.Open(); OracleTransaction transaction = connection.BeginTransaction();
try { for (int i = 0; i < SQLArrayList.Count; i++) { string SQLString = SQLArrayList[i].ToString(); OracleCommand command = new OracleCommand(SQLString, connection); command.ExecuteNonQuery(); } transaction.Commit(); return true; } catch (Exception ex) { transaction.Rollback(); Common.Global.strMessage = ex.Message.ToString(); return false; throw new Exception(ex.Message); } finally { connection.Close(); } } }
三、执行XmlType类型返回值的存储过程
public static string RunProcedures(string CunChuName, OracleParameter[] parm) { string strOut = ""; OracleCommand cmd = new OracleCommand(); using (OracleConnection conn = new OracleConnection(connectionString)) { try { cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = CunChuName; for (int i = 0; i < parm.Length; i++) { cmd.Parameters.Add(parm[i]); } if (conn.State != ConnectionState.Open) conn.Open();
cmd.Connection = conn; cmd.ExecuteNonQuery();
strOut = ((Oracle.ManagedDataAccess.Types.OracleXmlType)cmd.Parameters["Xml_Out"].Value).Value;
return strOut; } catch (Exception ex) { Common.Global.strMessage = ex.Message.ToString(); LogHelper.WriteErrorLog("CunChuName:" + CunChuName, ex); return strOut; } finally { if (conn.State == ConnectionState.Open) conn.Close(); conn.Dispose(); cmd.Parameters.Clear(); cmd.Dispose(); } }
}
该文章在 2025/7/11 14:41:22 编辑过