public OracleLob getOdb(OracleConnection conn, OracleTransaction tx, object imgBytes) { try { byte[] imgByte = (byte[])imgBytes; OracleCommand cmd = conn.CreateCommand(); cmd.Transaction = tx; //这里是关键,他定义了一个命令对象的t-sql语句,通过dmbs_lob来创建一个临时对象,这个对象的类型为blob,并存放在变量xx中,然后将xx的值付给外传参数tmpblob cmd.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;"; //构造外传参数对象,并加入到命令对象的参数集合中 cmd.Parameters.Add(new OracleParameter("tempblob", OracleType.Blob)).Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); //构造OracleLob对象,他的值为tmpblob外传参数的值 OracleLob tempLob = (OracleLob)cmd.Parameters[0].Value; //指定tempLob的访问模式,并开始操作二进制数据 tempLob.BeginBatch(OracleLobOpenMode.ReadWrite); //将二进制流byte数组集合写入到tmpLob里 tempLob.Write(imgByte, 0, imgByte.Length); tempLob.EndBatch(); return(tempLob); } catch (Exception ex) { CFunctions.HandleException(System.Reflection.MethodBase.GetCurrentMethod().Name, ex); return(null); } }
/// <summary> /// 处理大文本字符串。可能不完善,需要后续处理 /// </summary> /// <returns></returns> public override void SetCLOBVlaue(string txt, IDataParameter para, IDbConnection conn, IDbTransaction tran) { cmd = new OracleCommand(); if (tran != null) { cmd.Transaction = tran as OracleTransaction; } cmd.CommandText = "declare xx clob; begin dbms_lob.createtemporary(xx, false, 0); :tempclob := xx; end;"; cmd.Connection = conn as OracleConnection; OracleParameter p = new OracleParameter("tempclob", OracleType.Clob); p.Direction = ParameterDirection.Output; cmd.Parameters.Add(p); cmd.ExecuteNonQuery(); OracleLob tempLob = default(OracleLob); tempLob = (OracleLob)cmd.Parameters[0].Value; byte[] cbComments = Encoding.Unicode.GetBytes(txt); tempLob.BeginBatch(OracleLobOpenMode.ReadWrite); tempLob.Write(cbComments, 0, cbComments.Length); tempLob.EndBatch(); para.Value = tempLob; }
public static int RunAddProcedureBlob(bool isToCenter, string storedProcName, IDataParameter[] parameters, byte[] tempbuff) { string constring = ConfigurationManager.ConnectionStrings["Oracle"].ConnectionString; if (isToCenter) { constring = ConfigurationManager.ConnectionStrings["OracleCenter"].ConnectionString; } OracleConnection Connection = new OracleConnection(constring); Connection.Open(); OracleTransaction tx = Connection.BeginTransaction(); OracleCommand cmd = Connection.CreateCommand(); cmd.Transaction = tx; string type = " declare "; type = type + " xx Blob;"; string createtemp = type + " begin "; createtemp = createtemp + " dbms_lob.createtemporary(xx, false, 0); "; string setvalue = ""; setvalue = setvalue + ":templob := xx;"; cmd.CommandText = createtemp + setvalue + " end;"; cmd.Parameters.Add(new OracleParameter("templob", OracleType.Blob)).Direction = ParameterDirection.Output; cmd.ExecuteNonQuery(); OracleLob tempLob = (OracleLob)cmd.Parameters["templob"].Value; tempLob.BeginBatch(OracleLobOpenMode.ReadWrite); int abc = tempbuff.Length; double b = abc / 2; double a = Math.Ceiling(b); abc = (int)(a * 2); tempLob.Write(tempbuff, 0, abc); tempLob.EndBatch(); parameters[0].Value = tempLob; cmd.Parameters.Clear(); cmd.CommandText = storedProcName; cmd.CommandType = CommandType.StoredProcedure; foreach (OracleParameter parameter in parameters) { cmd.Parameters.Add(parameter); } cmd.ExecuteNonQuery(); int id = 0; if (cmd.Parameters.Count > 2) { id = Convert.ToInt32(cmd.Parameters[1].Value); } tx.Commit(); Connection.Close(); return(id); }
/// <summary> /// Logs an error to the database. /// </summary> /// <remarks> /// Use the stored procedure called by this implementation to set a /// policy on how long errors are kept in the log. The default /// implementation stores all errors for an indefinite time. /// </remarks> public override string Log(Error error) { if (error == null) { throw new ArgumentNullException("error"); } string errorXml = ErrorXml.EncodeString(error); Guid id = Guid.NewGuid(); using (OracleConnection connection = new OracleConnection(this.ConnectionString)) using (OracleCommand command = connection.CreateCommand()) { connection.Open(); using (OracleTransaction transaction = connection.BeginTransaction()) { // because we are storing the XML data in a NClob, we need to jump through a few hoops!! // so first we've got to operate within a transaction command.Transaction = transaction; // then we need to create a temporary lob on the database server command.CommandText = "declare xx nclob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;"; command.CommandType = CommandType.Text; OracleParameterCollection parameters = command.Parameters; parameters.Add("tempblob", OracleType.NClob).Direction = ParameterDirection.Output; command.ExecuteNonQuery(); // now we can get a handle to the NClob OracleLob xmlLob = (OracleLob)parameters[0].Value; // create a temporary buffer in which to store the XML byte[] tempbuff = Encoding.Unicode.GetBytes(errorXml); // and finally we can write to it! xmlLob.BeginBatch(OracleLobOpenMode.ReadWrite); xmlLob.Write(tempbuff, 0, tempbuff.Length); xmlLob.EndBatch(); command.CommandText = SchemaOwner + "pkg_elmah$log_error.LogError"; command.CommandType = CommandType.StoredProcedure; parameters.Clear(); parameters.Add("v_ErrorId", OracleType.NVarChar, 32).Value = id.ToString("N"); parameters.Add("v_Application", OracleType.NVarChar, _maxAppNameLength).Value = ApplicationName; parameters.Add("v_Host", OracleType.NVarChar, 30).Value = error.HostName; parameters.Add("v_Type", OracleType.NVarChar, 100).Value = error.Type; parameters.Add("v_Source", OracleType.NVarChar, 60).Value = error.Source; parameters.Add("v_Message", OracleType.NVarChar, 500).Value = error.Message; parameters.Add("v_User", OracleType.NVarChar, 50).Value = error.User; parameters.Add("v_AllXml", OracleType.NClob).Value = xmlLob; parameters.Add("v_StatusCode", OracleType.Int32).Value = error.StatusCode; parameters.Add("v_TimeUtc", OracleType.DateTime).Value = error.Time.ToUniversalTime(); command.ExecuteNonQuery(); transaction.Commit(); } return(id.ToString()); } }
public static OracleLob toBlob(OracleCommand cmd, byte[] byteContent) { OracleLob lob = createTempLob(cmd, (OracleType)2); lob.BeginBatch((OracleLobOpenMode)2); lob.Write(byteContent, 0, byteContent.Length); lob.EndBatch(); lob.Position = (0L); return(lob); }
public static OracleLob strToClob(OracleCommand cmd, string strSour) { OracleLob lob = createTempLob(cmd, (OracleType)4); byte[] bytes = new UnicodeEncoding().GetBytes(strSour); lob.BeginBatch((OracleLobOpenMode)2); lob.Write(bytes, 0, bytes.Length); lob.EndBatch(); return(lob); }
public static OracleLob toBlob(OracleCommand cmd, string fileName) { int num2; OracleLob lob = createTempLob(cmd, (OracleType)2); lob.BeginBatch((OracleLobOpenMode)2); FileStream stream = new FileStream(fileName, (FileMode)3); int num = 10240; byte[] buffer = new byte[10240]; while ((num2 = stream.Read(buffer, 0, num)) > 0) { lob.Write(buffer, 0, num2); } stream.Close(); lob.EndBatch(); lob.Position = (0L); return(lob); }
/// <summary> /// retrieve an Oracle dataset from an SP call /// </summary> /// <param name="conn"></param> /// <param name="strSPName"></param> /// <param name="ParamList"></param> /// <param name="lStatusCode"></param> /// <param name="strStatus"></param> /// <returns></returns> public bool GetOracleDataSet(CDataConnection conn, string strSPName, CParameterList ParamList, out DataSet ds, out k_STATUS_CODE lStatusCode, out string strStatus) { ds = null; lStatusCode = k_STATUS_CODE.Success; strStatus = String.Empty; k_STATUS_CODE lAuditStatusCode = k_STATUS_CODE.Success; string strAuditStatus = String.Empty; //return null if no conn if (conn == null) { ds = null; lStatusCode = k_STATUS_CODE.Failed; strStatus = "Unable to connect to data source, data connection is null!"; //audit the error if audting is on AuditTransaction(conn, strSPName, ParamList, lStatusCode, strStatus, out lAuditStatusCode, out strAuditStatus); return(false); } //create a new command object and set the command objects connection, text and type //must use OracleCommand or you cannot get back a ref cur out param which is how //we do things in medbase OracleCommand cmd = new OracleCommand(); // OleDbCommand(); cmd.Connection = conn.Conn; cmd.CommandText = strSPName; cmd.CommandType = CommandType.StoredProcedure; //add the parameters from the parameter list to the command parameter list for (int i = 0; i < ParamList.Count; i++) { CParameter parameter = ParamList.GetItemByIndex(i); if (parameter != null) { //create a new oledb param from our param and add it to the list //this follows how we currently do it in medbase //TODO: get direction, length etc from the parameter not hard coded below OracleParameter oraParameter = new OracleParameter(); oraParameter.ParameterName = parameter.ParameterName; //set the parameter value, default to string. if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.StringParameter) { oraParameter.Value = parameter.StringParameterValue; oraParameter.OracleType = OracleType.VarChar; oraParameter.Size = 4000; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.LongParameter) { oraParameter.Value = parameter.LongParameterValue; oraParameter.OracleType = OracleType.Int32; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.DoubleParameter) { oraParameter.Value = parameter.DoubleParameterValue; oraParameter.OracleType = OracleType.Double; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.DateParameter) { if (!CDataUtils.IsDateNull(parameter.DateParameterValue)) { //if the date is not null then set the value oraParameter.Value = parameter.DateParameterValue; } else { //set value to DBNull if date is null oraParameter.Value = DBNull.Value; } oraParameter.OracleType = OracleType.DateTime; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.CLOBParameter) { //You must begin a transaction before obtaining a temporary LOB. //Otherwise, the OracleDataReader may fail to obtain data later. OracleTransaction transaction = conn.Conn.BeginTransaction(); //make a new command OracleCommand command = conn.Conn.CreateCommand(); command.Connection = conn.Conn; command.Transaction = transaction; command.CommandText = "declare xx clob; begin dbms_lob.createtemporary(xx, false, 0); :tempclob := xx; end;"; command.Parameters.Add(new OracleParameter("tempclob", OracleType.Clob)).Direction = ParameterDirection.Output; command.ExecuteNonQuery(); //get a temp lob OracleLob tempLob = (OracleLob)command.Parameters[0].Value; //begin batch tempLob.BeginBatch(OracleLobOpenMode.ReadWrite); //convert string to byte array and write to lob, //note:encoding must be set to match oracle encoding //default encoding for oracle can be found by running //SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET' ; System.Text.ASCIIEncoding encASCII = new System.Text.ASCIIEncoding(); //ascii chars byte[] buffAsciiBytes = encASCII.GetBytes(parameter.CLOBParameterValue); //destination to convert to. convert from ascii to unicode UnicodeEncoding encDest = new UnicodeEncoding(); byte[] buffDest = Encoding.Convert(encASCII, encDest, buffAsciiBytes); //write the converted data to the lob tempLob.Write(buffDest, 0, buffDest.Length); //end batch tempLob.EndBatch(); //set the value of the param = lob oraParameter.OracleType = OracleType.Clob; //oraParameter.OracleType = OracleType.NClob; oraParameter.Value = tempLob; //all done so commit; transaction.Commit(); } else { oraParameter.Value = parameter.StringParameterValue; oraParameter.OracleType = OracleType.VarChar; oraParameter.Size = 4000; } oraParameter.Direction = parameter.Direction; cmd.Parameters.Add(oraParameter); } } //add in out params for stored proc, all sp's will return a status 1 = good, 0 = bad //status ParamList.AddOutputParameter("po_nStatusCode", 0); OracleParameter oraStatusParameter = new OracleParameter("po_nStatusCode", OracleType.Int32); oraStatusParameter.Direction = ParameterDirection.Output; cmd.Parameters.Add(oraStatusParameter); // //comment ParamList.AddOutputParameter("po_vStatusComment", String.Empty); OracleParameter oraCommentParameter = new OracleParameter("po_vStatusComment", OracleType.VarChar, 4000); oraCommentParameter.Direction = ParameterDirection.Output; cmd.Parameters.Add(oraCommentParameter); // //now add an out parameter to hold the ref cursor to the commands parameter list //returned ref cursor must always be named "RS" because OracleClient binds these //parameters by name, so you must name your parameter correctly //so the OracleParameter must be named the same thing. OracleParameter oraRSParameter = new OracleParameter("RS", OracleType.Cursor); //OracleType.Cursor oraRSParameter.Direction = ParameterDirection.Output; cmd.Parameters.Add(oraRSParameter); //create a new dataset to hold the conntent of the reference cursor ds = new DataSet(); //create an adapter and fill the dataset. //datasets are completely disconnected and provide //the most flexibility for later porting to a web service etc. try { OracleDataAdapter dataAdapter = new OracleDataAdapter(cmd); dataAdapter.Fill(ds); } catch (InvalidOperationException e) { ds = null; lStatusCode = k_STATUS_CODE.Failed; strStatus = e.Message; //audit if auditing is turned on AuditTransaction(conn, strSPName, ParamList, lStatusCode, strStatus, out lAuditStatusCode, out strAuditStatus); return(false); } catch (OracleException e) { ds = null; lStatusCode = k_STATUS_CODE.Failed; strStatus = e.Message; //audit if auditing is turned on AuditTransaction(conn, strSPName, ParamList, lStatusCode, strStatus, out lAuditStatusCode, out strAuditStatus); return(false); } if (lStatusCode == k_STATUS_CODE.Success) { //now read back out params into our list for (int i = 0; i < ParamList.Count; i++) { CParameter parameter = ParamList.GetItemByIndex(i); if (parameter != null) { if (parameter.Direction == ParameterDirection.Output || parameter.Direction == ParameterDirection.InputOutput) { foreach (OracleParameter oP in cmd.Parameters) { if (oP.ParameterName.Equals(parameter.ParameterName)) { if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.StringParameter) { if (oP.Value != null) { parameter.StringParameterValue = oP.Value.ToString(); } } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.LongParameter) { if (oP.Value != null) { if (!oP.Value.ToString().Equals(String.Empty)) { parameter.LongParameterValue = Convert.ToInt64(oP.Value); } } } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.DateParameter) { if (oP.Value != null) { if (!oP.Value.ToString().Equals(String.Empty)) { parameter.DateParameterValue = Convert.ToDateTime(oP.Value); } } } else { parameter.StringParameterValue = oP.Value.ToString(); } } } } } } } //set status code and text lStatusCode = ParamList.GetStatusCode(); strStatus = ParamList.GetStatusComment(); //audit if auditing is turned on if (!AuditTransaction(conn, strSPName, ParamList, lStatusCode, strStatus, out lAuditStatusCode, out strAuditStatus)) { //original error overrules audit error if (lStatusCode == k_STATUS_CODE.Success) { lStatusCode = lAuditStatusCode; strStatus = strAuditStatus; } } if (lStatusCode != k_STATUS_CODE.Success) { return(false); } return(true); }
/// <summary> /// Execute an oracle stored procedure using the /// parameters passed in /// </summary> /// <param name="strSPName"></param> /// <param name="ParamList"></param> /// <param name="lStatusCode"></param> /// <param name="strStatus"></param> /// <returns></returns> private bool ExecuteOracleSP(bool bAudit, string strSPName, CParameterList ParamList, out k_STATUS_CODE lStatusCode, out string strStatus) { lStatusCode = k_STATUS_CODE.Success; strStatus = String.Empty; //return null if no conn if (m_OracleConnection == null) { lStatusCode = k_STATUS_CODE.Failed; strStatus = "Unable to connect to data source, Data Connection is null"; return(false); } //create a new command object and set the command objects connection, text and type //must use OracleCommand or you cannot get back a ref cur out param which is how //we do things in medbase OracleCommand cmd = new OracleCommand(); cmd.Connection = m_OracleConnection; cmd.CommandText = strSPName; cmd.CommandType = CommandType.StoredProcedure; //add the parameters from the parameter list to the command parameter list for (int i = 0; i < ParamList.Count; i++) { CParameter parameter = ParamList.GetItemByIndex(i); if (parameter != null) { //create a new oledb param from our param and add it to the list //this follows how we currently do it in medbase OracleParameter oraParameter = new OracleParameter(); oraParameter.ParameterName = parameter.ParameterName; //set the parameter value, default to string. Probably a better way than the //if then else, but this works and we can find it later, if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.StringParameter) { oraParameter.Value = parameter.StringParameterValue; oraParameter.OracleType = OracleType.VarChar; oraParameter.Size = 4000; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.LongParameter) { oraParameter.Value = parameter.LongParameterValue; oraParameter.OracleType = OracleType.Int32; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.DoubleParameter) { oraParameter.Value = parameter.DoubleParameterValue; oraParameter.OracleType = OracleType.Double; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.IntParameter) { oraParameter.Value = parameter.IntParameterValue; oraParameter.OracleType = OracleType.Int32; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.DateParameter) { oraParameter.Value = parameter.DateParameterValue; if (!CDataUtils.IsDateNull(parameter.DateParameterValue)) { //if the date is not null then set the value oraParameter.Value = parameter.DateParameterValue; } else { //set value to DBNull if date is null oraParameter.Value = DBNull.Value; } oraParameter.OracleType = OracleType.DateTime; } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.CLOBParameter) { //You must begin a transaction before obtaining a temporary LOB. //Otherwise, the OracleDataReader may fail to obtain data later. OracleTransaction transaction = m_OracleConnection.BeginTransaction(); //make a new command OracleCommand command = m_OracleConnection.CreateCommand(); command.Connection = m_OracleConnection; command.Transaction = transaction; command.CommandText = "declare xx clob; begin dbms_lob.createtemporary(xx, false, 0); :tempclob := xx; end;"; command.Parameters.Add(new OracleParameter("tempclob", OracleType.Clob)).Direction = ParameterDirection.Output; command.ExecuteNonQuery(); //get a temp lob OracleLob tempLob = (OracleLob)command.Parameters[0].Value; //begin batch tempLob.BeginBatch(OracleLobOpenMode.ReadWrite); //convert string to byte array and write to lob, //note:encoding must be set to match oracle encoding //default encoding for oracle can be found by running //SELECT value$ FROM sys.props$ WHERE name = 'NLS_CHARACTERSET' ; System.Text.ASCIIEncoding encASCII = new System.Text.ASCIIEncoding(); //ascii chars byte[] buffAsciiBytes = encASCII.GetBytes(parameter.CLOBParameterValue); //destination to convert to. convert from ascii to unicode UnicodeEncoding encDest = new UnicodeEncoding(); byte[] buffDest = Encoding.Convert(encASCII, encDest, buffAsciiBytes); //write the converted data to the lob tempLob.Write(buffDest, 0, buffDest.Length); //end batch tempLob.EndBatch(); //set the value of the param = lob oraParameter.OracleType = OracleType.Clob; //oraParameter.OracleType = OracleType.NClob; oraParameter.Value = tempLob; //all done so commit; transaction.Commit(); } else { oraParameter.Value = parameter.StringParameterValue; oraParameter.OracleType = OracleType.VarChar; oraParameter.Size = 4000; } oraParameter.Direction = parameter.Direction; cmd.Parameters.Add(oraParameter); } } //add in out params for stored proc, all sp's will return a status 0 = good, 1 = bad // //status ParamList.AddOutputParameter("po_nStatusCode", 0); OracleParameter oraStatusParameter = new OracleParameter("po_nStatusCode", OracleType.Int32); oraStatusParameter.Direction = ParameterDirection.Output; cmd.Parameters.Add(oraStatusParameter); // //comment ParamList.AddOutputParameter("po_vStatusComment", String.Empty); OracleParameter oraCommentParameter = new OracleParameter("po_vStatusComment", OracleType.VarChar, 4000); oraCommentParameter.Direction = ParameterDirection.Output; cmd.Parameters.Add(oraCommentParameter); // try { //execute the stored proc and move the out param values back into //our list cmd.ExecuteNonQuery(); for (int i = 0; i < ParamList.Count; i++) { CParameter parameter = ParamList.GetItemByIndex(i); if (parameter != null) { if (parameter.Direction == ParameterDirection.Output || parameter.Direction == ParameterDirection.InputOutput) { foreach (OracleParameter oP in cmd.Parameters) { if (oP.ParameterName.Equals(parameter.ParameterName)) { if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.StringParameter) { if (oP.Value != null) { parameter.StringParameterValue = oP.Value.ToString(); parameter.StringParameterValue = parameter.StringParameterValue.Trim(); } } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.LongParameter) { if (oP.Value != null) { if (oP.Value.ToString() != String.Empty) { parameter.LongParameterValue = Convert.ToInt64(oP.Value); } } } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.DoubleParameter) { if (oP.Value != null) { if (oP.Value.ToString() != String.Empty) { parameter.DoubleParameterValue = Convert.ToDouble(oP.Value); } } } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.BoolParameter) { if (oP.Value != null) { if (oP.Value.ToString() != String.Empty) { parameter.BoolParameterValue = Convert.ToBoolean(oP.Value); } } } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.IntParameter) { if (oP.Value != null) { if (oP.Value.ToString() != String.Empty) { parameter.IntParameterValue = Convert.ToInt32(oP.Value); } } } else if (parameter.ParameterType == k_DATA_PARAMETER_TYPE.DateParameter) { if (oP.Value != null) { if (oP.Value.ToString() != String.Empty) { if (!oP.Value.ToString().Equals(String.Empty)) { parameter.DateParameterValue = Convert.ToDateTime(oP.Value); } } } } else { if (oP.Value != null) { parameter.StringParameterValue = oP.Value.ToString(); parameter.StringParameterValue = parameter.StringParameterValue.Trim(); } } } } } } } lStatusCode = ParamList.GetStatusCode(); strStatus = ParamList.GetStatusComment(); if (lStatusCode == k_STATUS_CODE.Success) { return(true); } return(false); } catch (InvalidOperationException e) { strStatus = e.Message; lStatusCode = k_STATUS_CODE.Failed; } catch (OracleException e) { strStatus = e.Message; lStatusCode = k_STATUS_CODE.Failed; } return(false); }