(1)首先 需要创建序列:
CREATE SEQUENCE SE_TD_POWER
MINVALUE 1
NOMAXVALUE
START WITH 1
INCREMENT BY 1
NOCYCLE
CACHE 30
/
(2)然后 需要创建存储过程,
使插入一条数据 strInsertSQL,
同时查询当前的ID select SE_TD_POWER.currval from dual ,
/* 存储过程
create or replace procedure PRO_GetInsertedID(
strInsertSQL varchar2,
seqName varchar2,
ID out NUMBER
)
is
strSql varchar(200);
begin
execute immediate strInsertSQL;--执行插入语句
strSql:='select '||seqName||'.currval from dual';--获取id的sql语句
execute immediate strSql into ID;--执行获取id的sql语句
EXCEPTION
WHEN OTHERS THEN
DBMS_output.PUT_LINE(SQLERRM);
end PRO_GetInsertedID;
*/
(3)C#中执行插入操作:
using Oracle.DataAccess.Client; using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DIDAO.DAL { public class OracleHelper { //链接字符串 private static readonly string connStr = ConfigurationManager.ConnectionStrings["dbconnStr"].ConnectionString; /// <summary> /// 创建链接 /// </summary> /// <returns>链接</returns> public static OracleConnection CreateConnection() { OracleConnection conn = new OracleConnection(connStr); conn.Open(); return conn; } /// <summary> /// 使用亦有链接的 非查询 /// </summary> /// <param name="conn">链接</param> /// <param name="sql">sql文本</param> /// <param name="parameters">sql参数</param> /// <returns>受影响行数</returns> public static int ExecuteNonQuery(OracleConnection conn,string sql,params OracleParameter[] parameters) { using(OracleCommand cmd=new OracleCommand(sql,conn)) { cmd.Parameters.AddRange(parameters); return cmd.ExecuteNonQuery(); } } /// <summary> /// 自己创建链接的 非查询 /// </summary> /// <param name="sql">sql文本</param> /// <param name="parameters">sql参数</param> /// <returns>受影响行数</returns> public static int ExecuteNonQuery(string sql, params OracleParameter[] parameters) { using (OracleConnection conn = CreateConnection()) { return ExecuteNonQuery(conn, sql, parameters); } } /// <summary> /// 使用已有链接的 带存储过程的Insert非查询,直接写存储过程参数 /// </summary> /// <param name="conn">已有链接</param> /// <param name="proName">存储过程名称</param> /// <param name="strInsertSQL">执行插入的sql语句,或者其他操作sql语句</param> /// <param name="seqName">序列的名称</param> /// <returns>当前序列号,即ID</returns> public static object ExecuteNonQueryWithProduce(OracleConnection conn, string proName, string strInsertSQL, string seqName) { using (OracleCommand cmd = new OracleCommand(proName, conn)) //命令中执行的不在是sql,而是存储过程 { try { cmd.CommandType = CommandType.StoredProcedure; //标记该命令的类型不是sql,而是存储过程 //存储过程中有参数名称,以及设置对应参数的值 cmd.Parameters.Add(new OracleParameter("strInsertSQL", OracleDbType.Varchar2) { Value = strInsertSQL }); ////存储过程中的参入参数 strInsertSQL cmd.Parameters.Add(new OracleParameter("seqName", OracleDbType.Varchar2) { Value = seqName }); // //存储过程中的传入参数 seqName cmd.Parameters.Add(new OracleParameter("ID", OracleDbType.Int64) { Direction = ParameterDirection.Output }); //存储过程中的传出参数ID,只需要声明 //cmd.Parameters.AddRange(parameters); cmd.ExecuteNonQuery(); string newId = cmd.Parameters["ID"].Value.ToString(); //获得传出参数的ID的值 return newId; } catch(Exception ex) { throw new Exception(ex.ToString()); } } } /// <summary> /// 自己创建链接的 带存储过程的Insert非查询,直接写存储过程参数 /// </summary> /// <param name="proName">存储过程名称</param> /// <param name="strInsertSQL">执行插入的sql语句,或者其他操作sql语句</param> /// <param name="seqName">序列的名称</param> /// <returns>当前序列号,即ID</returns> public static object ExecuteNonQueryWithProduce(string proName, string strInsertSQL, string seqName) { using (OracleConnection conn = CreateConnection()) { return ExecuteNonQueryWithProduce(conn, proName, strInsertSQL, seqName); } } /// <summary> /// 使用已有链接的 带存储过程的Insert非查询,传存储过程参数 /// </summary> /// <param name="conn">已有链接</param> /// <param name="proName">存储过程名称</param> /// <param name="parameters">存储过程中的传入、传出参数 数组</param> /// <returns>当前序列号,即ID</returns> public static object ExecuteNonQueryWithProduce(OracleConnection conn, string proName, params OracleParameter[] parameters) { using (OracleCommand cmd = new OracleCommand(proName, conn)) //命令中执行的不在是sql,而是存储过程 { try { cmd.CommandType = CommandType.StoredProcedure; //标记该命令的类型不是sql,而是存储过程 ////存储过程中有参数名称,以及设置对应参数的值 //cmd.Parameters.Add(new OracleParameter("strInsertSQL", OracleDbType.Varchar2) { Value = strInsertSQL }); ////存储过程中的参入参数 strInsertSQL //cmd.Parameters.Add(new OracleParameter("seqName", OracleDbType.Varchar2) { Value = seqName }); // //存储过程中的传入参数 seqName //cmd.Parameters.Add(new OracleParameter("ID", OracleDbType.Int32) { Direction = ParameterDirection.Output }); //存储过程中的传出参数ID,只需要声明 cmd.Parameters.AddRange(parameters); //参数中包括存储过程的传入传出参数,以及子sql语句中的参数 --------------****----------------- int i = cmd.ExecuteNonQuery(); //直接返回执行插入之后,存储过程传出的变量值 string newId = cmd.Parameters["ID"].Value.ToString(); //获得传出参数的ID的值 return newId; } catch (Exception ex) { throw new Exception(ex.ToString()); } } } /// <summary> /// 自己创建链接的 带存储过程的Insert非查询,传存储过程参数 /// </summary> /// <param name="proName">存储过程名称</param> /// <param name="parameters">存储过程中的传入、传出参数 数组</param> /// <returns>当前序列号,即ID</returns> public static object ExecuteNonQueryWithProduce(string proName,params OracleParameter[] parameters) { using (OracleConnection conn = CreateConnection()) { return ExecuteNonQueryWithProduce(conn, proName, parameters); } } /// <summary> /// 使用已有链接的 单查询 /// </summary> /// <param name="conn">链接</param> /// <param name="sql">sql文本</param> /// <param name="parameters">sql参数</param> /// <returns>查询到的一条结果</returns> public static object ExecuteScalar(OracleConnection conn,string sql,params OracleParameter[] parameters) { using(OracleCommand cmd=new OracleCommand(sql,conn)) { cmd.Parameters.AddRange(parameters); return cmd.ExecuteScalar(); } } /// <summary> /// 自己创建链接的 单查询 /// </summary> /// <param name="sql">sql文本</param> /// <param name="parameters">sql参数</param> /// <returns>查询到的一条结果</returns> public static object ExecuteScalar(string sql,params OracleParameter[] parameters) { using(OracleConnection conn=CreateConnection()) { return ExecuteScalar(conn, sql, parameters); } } /// <summary> /// 使用已有链接的 reader查询 /// </summary> /// <param name="conn">链接</param> /// <param name="sql">sql文本</param> /// <param name="parameters">sql参数</param> /// <returns>查询到的结果集table</returns> public static DataTable ExecuteReader(OracleConnection conn,string sql,params OracleParameter[] parameters) { DataTable table = new DataTable(); using(OracleCommand cmd=new OracleCommand(sql,conn)) { cmd.Parameters.AddRange(parameters); using (OracleDataReader reader = cmd.ExecuteReader()) { table.Load(reader); } } return table; } /// <summary> /// 自己创建链接的 reader查询 /// </summary> /// <param name="sql">sql文本</param> /// <param name="parameters">sql参数</param> /// <returns>查询到的结果集table</returns> public static DataTable ExecuteReader(string sql,params OracleParameter[] parameters) { using(OracleConnection conn=CreateConnection()) { return ExecuteReader(conn, sql, parameters); } } /// <summary> /// 使用已有链接的 stream查询 /// </summary> /// <param name="conn">链接</param> /// <param name="sql">sql文本</param> /// <param name="parameters">sql参数</param> /// <returns>查询到的结果流stream</returns> public static System.IO.Stream ExecuteStream(OracleConnection conn,string sql,params OracleParameter[] parameters) { using(OracleCommand cmd=new OracleCommand(sql,conn)) { cmd.Parameters.AddRange(parameters); using (System.IO.Stream stream = cmd.ExecuteStream()) { return stream; } } } /// <summary> /// 自己创建链接的stream查询 /// </summary> /// <param name="sql">sql文本</param> /// <param name="parameters">sql参数</param> /// <returns>查询到的结果流stream</returns> public static System.IO.Stream ExecuteStream(string sql, params OracleParameter[] parameters) { using(OracleConnection conn=CreateConnection()) { return ExecuteStream(conn, sql, parameters); } } } }
/// <summary> /// 调用存储过程进行新增,返回ID (---> isTimestamp 表示有tiemstamp类型) /// </summary> /// <param name="obj">实例</param> /// <param name="seqName">存储过程中的参数-序列,sql语句中参数-序列</param> /// <param name="proName">存储过程名称</param> /// <param name="isTimestamp">是否有Oracle中的Timestamp类型 (只要是Oracle不论Date/Timestamp 都可以to_timetamp() : true)</param> /// <returns>返回当前插入语句的ID</returns> public object InsertModelWithProduce(Object obj, string seqName, string proName, bool isTimestamp) { string className = obj.GetType().Name; List<OracleParameter> produceParaList = new List<OracleParameter>(); StringBuilder sb = new StringBuilder(); sb.Append("INSERT INTO ").Append(className).Append(" VALUES(").Append(seqName).Append(".NEXTVAL,").Append(JoinPropValueStr(obj, isTimestamp)).AppendLine(")"); string strInsertSQL = sb.ToString(); #region 冗余 ////return OracleHelper.ExecuteNonQuery(sql, paraList.ToArray()); ////object id = OracleHelper.ExecuteNonQuery("PR_GETITEMID", sql, "SE_T_ALL"); ////首先 sql语句中的参数 sql语句中SE_T_ALL参数 必须位于集合的第一位 ,序列名称在存储过程中已有传参 #endregion //只传 存储过程中的参数 produceParaList.AddRange(new List<OracleParameter>(){ new OracleParameter("strInsertSQL", OracleDbType.Varchar2) { Value = strInsertSQL }, //存储过程中的参入参数 strInsertSQL new OracleParameter("seqName", OracleDbType.Varchar2) { Value = seqName }, //存储过程中的传入参数 seqName new OracleParameter("ID", OracleDbType.Int64) { Direction=ParameterDirection.Output} //存储过程中的传出参数ID,只需要声明 }); object id = OracleHelper.ExecuteNonQueryWithProduce(proName, produceParaList.ToArray()); return id; } /// <summary> /// 调用存储过程进行新增,返回ID (sql直接拼接,没有参数 ---> isTimestamp 表示有tiemstamp类型) /// </summary> /// <param name="proName">存储过程名称</param> /// <param name="strInsertSQL">执行插入的sql语句,或者其他操作sql语句</param> /// <param name="seqName">序列的名称</param> /// <param name="isTimestamp">是否有Oracle中的Timestamp类型 (只要是Oracle不论Date/Timestamp 都可以to_timetamp() : true)</param> /// <returns>当前序列号,即ID</returns> public object InsertModelWithProduceForDirectSQL(Object obj, string seqName, string proName,bool isTimestamp) { string className = obj.GetType().Name; List<OracleParameter> produceParaList = new List<OracleParameter>(); StringBuilder sb = new StringBuilder(); sb.Append("INSERT INTO ").Append(className).Append(" VALUES(").Append(seqName).Append(".NEXTVAL,").Append(JoinPropValueStr(obj,isTimestamp)).AppendLine(")"); string strInsertSQL = sb.ToString(); return OracleHelper.ExecuteNonQueryWithProduce(proName, strInsertSQL, seqName); } /// <summary> /// 拼接新增sql语句中的列的value字符串 (isTimestamp :有timestamp类型为true) /// </summary> /// <param name="obj">实例</param> /// <param name="isTimestamp">是否有Oracle中的Timestamp类型 (只要是Oracle不论Date/Timestamp 都可以to_timetamp() : true)</param> /// <returns>新增sql语句中的列的value字符串</returns> private string JoinPropValueStr(Object obj,bool isTimestamp) { string propValueStrs = ""; Type type = obj.GetType(); PropertyInfo[] props = type.GetProperties(); foreach (PropertyInfo prop in props) { string propName = prop.Name; if (propName != "ID") { string propTyName = prop.PropertyType.Name; //属性类别名称 object propValue = prop.GetValue(obj); if (propValue == null || propValue.ToString().Length <= 0) { propValueStrs += "null,"; } else { if (propTyName == "String") { propValueStrs += "'" + propValue + "',"; } else if (propTyName == "DateTime" || propTyName == "DateTime?") { if ( isTimestamp) //如果Oracle中Date类型是 timestamp类型,需要 to_timestamp() /Date类型也可以to_timestamp() { //to_timestamp('1990-09-24','yyyy-mm-dd hh24:mi:ss.ff'), propValueStrs += "to_timestamp('" + propValue + "','yyyy-mm-dd hh24:mi:ss.ff'),"; } else { propValueStrs += "'" + propValue + "',"; //日期的格式必须是 YYYY-MM-DD 或者 yyyy-mm-dd hh24:MI:SS } } else { propValueStrs += propValue + ","; } } } } propValueStrs = propValueStrs.TrimEnd(','); //去掉最后的"," return propValueStrs; }
/// <summary> /// 调用存储过程进行新增,返回ID (---> isTimestamp 表示有tiemstamp类型) /// </summary> /// <param name="obj">实例</param> /// <param name="seqName">存储过程中的参数-序列,sql语句中参数-序列</param> /// <param name="proName">存储过程名称</param> /// <param name="isTimestamp">是否有Oracle中的Timestamp类型 (只要是Oracle不论Date/Timestamp 都可以to_timetamp() : true)</param> /// <returns>返回当前插入语句的ID</returns> public object InsertModelWithProduce(Object obj, string seqName, string proName, bool isTimestamp) { return myORM_DAL.InsertModelWithProduce(obj, seqName, proName,isTimestamp); } /// <summary> /// 调用存储过程进行新增,返回ID (sql直接拼接,没有参数 ---> isTimestamp 表示有tiemstamp类型) /// </summary> /// <param name="proName">存储过程名称</param> /// <param name="strInsertSQL">执行插入的sql语句,或者其他操作sql语句</param> /// <param name="seqName">序列的名称</param> /// <param name="isTimestamp">是否有Oracle中的Timestamp类型 (只要是Oracle不论Date/Timestamp 都可以to_timetamp() : true)</param> /// <returns>当前序列号,即ID</returns> public object InsertModelWithProduceForDirectSQL(Object obj, string seqName, string proName, bool isTimestamp) { return myORM_DAL.InsertModelWithProduceForDirectSQL(obj, seqName, proName, isTimestamp); }