2014-10-01 102 views

回答

1

與流一個答案:

string path = @"D:\testfile.txt"; 
System.IO.FileStream myStream = new System.IO.FileStream(@path, FileMode.Open); 

BinaryReader binaryReader = new BinaryReader(myStream); 
byte[] data = binaryReader.ReadBytes((int)myStream.Length); //read the stream into byte 

String sql = "INSERT INTO testblob (testid, testblob) VALUES (100, :blobtodb)";  

OracleCommand cmd = new OracleCommand(); 
cmd.CommandText = sql; // Set the sql-command 
cmd.Connection = con; //con is an OracleConnection, create it before 

OracleParameter param = cmd.Parameters.Add("blobtodb", OracleDbType.Blob); //Add the parameter for the blobcolumn 
param.Direction = ParameterDirection.Input; 

param.Value = data; //Asign the Byte Array to the parameter 
cmd.ExecuteNonQuery(); //You are done! 

這應該與每一個流工作;)


有關文件(超過)1TB:

警告:使用大文件的有機磷農藥的解決方案,因爲它是真的很難有1TB內存;)

老實說,我不知道,我不能這麼大的文件,在這裏嘗試。然而BLOB s是specified by Oracle與:Maximum size: (4 GB - 1) * DB_BLOCK_SIZE initialization parameter (8 TB to 128 TB)。所以從數據庫端它應該工作。 但是你MAYBE需要注意Connection TimeoutConnection Lifetime,因爲在網絡上傳輸這樣一個大文件需要很長時間(我猜,我不知道你的設置)。

+0

可以用1TB文件嗎? – 2014-10-02 16:38:06

+0

@OtávioDécio查看我的更新回答。 – DatRid 2014-10-06 09:07:39

+0

其實我想我找到了我在找的東西 - 你需要創建一個OracleBlob,將你的大內容流入它(Oracle將它流入數據庫),然後將OracleBlob作爲參數分配給連接,在此時它實際上寫到表 – 2014-10-06 13:48:07

0

是,設置你的參數了,像這樣:

var param = cmd.Parameters.Add("blobInParam", OracleDbType.Blob); 
param.Direction = ParameterDirection.Input; 

// Assign Byte Array to Oracle Parameter 
param.Value = blobData; 
+0

而我的腦海裏,字節必須是偶數,對不對? *只是添加,也許值得去了解* – DatRid 2014-10-01 15:42:21

+0

不能誠實地說,我見過這種限制;但這並不意味着它不在那裏! – 2014-10-01 15:45:34

+0

如何設置二進制流到blob,如OutputStream outstream = blob.setBinaryStream(1L); ? – 2014-10-01 16:13:37

相關問題