Oracle BLOB with asp.net

21 篇文章 0 订阅
19 篇文章 0 订阅

Oracle BLOB with asp.net 

http://msdn.microsoft.com/en-US/library/cydxhzhz(v=VS.80).aspx



cannot create a Binary Large Object (BLOB) from a byte array

Using the code above, I get the error:
"invalid operation. the connection is closed"

http://support.microsoft.com/default.aspx?scid=kb;en-us;322796

the key: "declare xx blob; begin dbms_lob.createtemporary(xx, false, 0); :tempblob := xx; end;"

It is a misnomer to assume we will pre-populate using "INSERT INTO tablewithlobs values (1, 'AA', 'AAA', N'AAAA')";

Here is the VB.NET code from the Microsoft site that worked for me, followed by a C# example I had found first from Experts Exchange:

Dim conn As New OracleConnection("server=Oracle;Uid=uid;pwd=pwd")
Dim filePath As String
Dim bigData As Byte()
Dim t As Date

t = Now

filePath = "C:\mytest.bmp" 'Add the path to the file you want to insert
If Not File.Exists(filePath) Then
' handle error
End If


Dim fs As Stream = File.OpenRead(filePath)
Dim tempBuff(fs.Length) As Byte

fs.Read(tempBuff, 0, fs.Length)
fs.Close()
conn.Open()

Dim tx As OracleTransaction
tx = conn.BeginTransaction()

Dim cmd As New OracleCommand()
cmd = conn.CreateCommand()

cmd.Transaction = tx

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()



Dim tempLob As OracleLob
tempLob = cmd.Parameters(0).Value
tempLob.BeginBatch(OracleLobOpenMode.ReadWrite)
tempLob.Write(tempBuff, 0, tempBuff.Length)
tempLob.EndBatch()

cmd.Parameters.Clear()
cmd.CommandText = "InsertBlob.TestBlobInsert"
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.Add(New OracleParameter("BlobParam", OracleType.Blob)).Value = tempLob

Try
cmd.ExecuteNonQuery()
Catch myex As Exception
MsgBox(myex.Message)
End Try

tx.Commit()

--------------------------------------

From "ofaniel" on Experts Exchange where I found it first...

Here is a similar method in C# ... at the veeeery bottom of this Experts Exchange page:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_24230043.html

private void UpdateCaseNotes(string SchemaReference, string DefectId, string Comments)
{
using (OracleConnection conn = new OracleConnection(ConfigurationSettings.AppSettings["DBConnStr"]))
{
conn.Open();

OracleCommand cmd = new OracleCommand();
cmd = conn.CreateCommand();

OracleTransaction tx = conn.BeginTransaction();
cmd.Transaction = tx;

cmd.CommandText = "declare xx clob; begin dbms_lob.createtemporary(xx, false, 0); :tempclob := xx; end;";
cmd.Parameters.Add(new OracleParameter("tempclob", OracleType.Clob)).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();

byte[] cbComments = Encoding.Unicode.GetBytes(Comments);

OracleLob tmpClob = default(OracleLob);
tmpClob = (OracleLob)cmd.Parameters[0].Value;
tmpClob.BeginBatch(OracleLobOpenMode.ReadWrite);
tmpClob.Write(cbComments, 0, cbComments.Length);
tmpClob.EndBatch();

cmd.Parameters.Clear();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "UPDATE " + SchemaReference + ".BUG SET " + colCaseComment + " = :clb WHERE BG_BUG_ID = " + DefectId;
cmd.Parameters.Add(new OracleParameter("clb", OracleType.Clob)).Value = tmpClob;

try
{
cmd.ExecuteNonQuery();
tx.Commit();
}
catch (OracleException ex)
{
throw new Exception("Error updating case comment.", ex);
}
finally
{
cmd.Dispose();
tx.Dispose();
conn.Close();
}
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值