如何使用c#在Oracle表中上传任意类型和大小的文件

本文关键字:类型 任意 文件 何使用 Oracle | 更新日期: 2023-09-27 18:05:24

我想上传任何类型的文件在oracle表列和文件大小到blob数据类型列的oracle。文件夹有权限

如何使用c#在Oracle表中上传任意类型和大小的文件

//这里首先你需要上传文件到应用服务器上,然后转换成文件流。

string auditReportUploadDocLocation = ConfigurationManager.AppSettings["AuditReportUploadDocLocation"].ToString();
        string filelocation = Path.Combine(Server.MapPath(auditReportUploadDocLocation), fuUploadDoc.FileName);
        fuUploadDoc.PostedFile.SaveAs(filelocation);
    fileName = Path.GetFileName(fuUploadDoc.PostedFile.FileName);
    using (FileStream fs = new FileStream(filelocation, FileMode.Open, FileAccess.Read))
        {
               objAudAuditReportMaster = objAudAuditReportBAL.UploadAuditReportDoc(fileName, fs, Session["AudLoginID"].ToString(), audProcessName);
        }

//删除应用服务器上的文件

if (File.Exists(filelocation))
    File.Delete(filelocation);

//之后调用这个函数

public AudAuditReportMaster UploadAuditReportDoc(string fileName, FileStream fs, string modifyBy, string processName)
       {
           AudAuditReportMaster objAudAuditReportMaster = new AudAuditReportMaster();
            BinaryReader brFileStream = new BinaryReader(fs);
            OpenConnection();
            int streamLength = (int)fs.Length;
            OracleTransaction transaction = objConn.BeginTransaction();
            objCmd.Transaction = transaction;
            objCmd.CommandText = "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;";
            objCmd.Parameters.Add(new OracleParameter("tempblob", OracleType.Blob)).Direction = ParameterDirection.Output;
            objCmd.ExecuteNonQuery();
            OracleLob tempLob = (OracleLob)objCmd.Parameters[0].Value;
            byte[] tempbuff = new byte[streamLength];
            tempLob.BeginBatch(OracleLobOpenMode.ReadWrite);
            tempLob.Write(brFileStream.ReadBytes(streamLength), 0, streamLength);
            tempLob.EndBatch();
            objCmd.Parameters.Clear();
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.CommandText = AudDB_Constants.Aud_Audit_Pkg.SP_Aud_Upload_Doc;
            objCmd.Parameters.Clear();
            OracleParameter param1 = objCmd.Parameters.Add("p_doc_name", OracleType.VarChar);
            param1.Value = fileName;
            objCmd.Parameters.Add(new OracleParameter("p_process_name", OracleType.VarChar)).Value = processName;
            objCmd.Parameters.Add(new OracleParameter("p_process_key_name", OracleType.VarChar)).Value = DBNull.Value;
            objCmd.Parameters.Add(new OracleParameter("p_process_key_id", OracleType.Int32)).Value = DBNull.Value;
            OracleParameter param2 = objCmd.Parameters.Add("p_document", OracleType.Blob);
            param2.Value = tempLob;
            OracleParameter param3 = objCmd.Parameters.Add("p_last_updated_by", OracleType.VarChar);
            param3.Value = modifyBy;
            objCmd.Parameters.Add(new OracleParameter("p_attribute1", OracleType.VarChar)).Value = DBNull.Value;
            objCmd.Parameters.Add(new OracleParameter("p_attribute2", OracleType.VarChar)).Value = DBNull.Value;
            objCmd.Parameters.Add(new OracleParameter("p_attribute3", OracleType.VarChar)).Value = DBNull.Value;
            objCmd.Parameters.Add(new OracleParameter("p_attribute4", OracleType.VarChar)).Value = DBNull.Value;
            objCmd.Parameters.Add(new OracleParameter("p_attribute5", OracleType.VarChar)).Value = DBNull.Value;
            OracleParameter param4 = objCmd.Parameters.Add("p_doc_id", OracleType.Int32);
            param4.Direction = ParameterDirection.Output;
            OracleParameter param5 = objCmd.Parameters.Add("p_status", OracleType.VarChar, 100);
            param5.Direction = ParameterDirection.Output;
            OracleParameter param6 = objCmd.Parameters.Add("p_status_dtl", OracleType.VarChar, 1000);
            param6.Direction = ParameterDirection.Output;
            objAudAuditReportMaster.Status = Convert.ToString(objCmd.ExecuteNonQuery());
            objAudAuditReportMaster.DocID = Convert.ToInt32(param4.Value.ToString());
            objAudAuditReportMaster.Status = param5.Value.ToString();
            objAudAuditReportMaster.StatusDetail = param6.Value.ToString();
            transaction.Commit();
            CloseConnection();
            DisposeConnection();
            return objAudAuditReportMaster;
        }

//在数据库端调用这个过程

procedure aud_upload_doc(p_doc_name                 in  varchar2,
                         p_process_name             in  varchar2,
                         p_process_key_name         in  varchar2,
                         p_process_key_id           in  number,
                         p_document                 in  blob,    
                         p_last_updated_by          in  varchar2, 
                         p_doc_id                   out number,
begin
      p_doc_id := AUD_UNIQUE_VAL_SEQ.nextval; 
         insert into AUD_AUDIT_UPLOAD_DOC(doc_id,   doc_name,   process_name,   process_key_name,   process_key_id,   doc,        created_by,        creation_date,  last_updated_by,   last_update_date )
                                  values (p_doc_id, p_doc_name, p_process_name, p_process_key_name, p_process_key_id, p_document, p_last_updated_by, sysdate,        p_last_updated_by, sysdate        );
end;