在.Net中更新查询时,我做错什么了吗?我遇到了错误,这是我知道的唯一方法。还有什么其他方法可以将字节数组文档更新为oracle数据库。我在调试时检查的条件都很好。
Dim data As Byte() = lDS.Rows(0)(pFieldName)
Dim strData As String
Using fs As New FileStream(pFileName, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim br As New StreamReader(fs)
strData = br.ReadToEnd()
br.Close()
End Using
Dim oradb As String
oradb = String.Format("Data Source={0};User Id={1};Password={2};", lOracleDB, lUserID, lPassword)
Dim conn As New OracleConnection(oradb)
conn.Open()
Dim strCondition As String = pSQL.ToLower()
strCondition = strCondition.Substring(pSQL.IndexOf("where"), pSQL.Length - pSQL.IndexOf("where") - 1)
Dim sql As String = "UPDATE " + tableName + " SET " + pFieldName + " = :" + pFieldName + " " + strCondition
Dim parmData As OracleParameter = New OracleParameter
parmData.Direction = ParameterDirection.Input
parmData.OracleType = OracleType.Clob
parmData.Size = strData.Length
parmData.ParameterName = pFieldName
parmData.Value = strData
Dim cm As OracleCommand = New OracleCommand
cm.Connection = conn
cm.Parameters.Add(parmData)
cm.CommandText = sql
'At below line I am getting the error
cm.ExecuteNonQuery()
conn.Close()
答案 0 :(得分:0)
您的代码非常冗长。详细的代码不是错误的位,它往往更难阅读,因此更容易出错。如果要将二进制数据从文件保存到数据库,则可以这样简单地完成操作:
Dim data = File.ReadAllBytes(filePath)
Using connection As New OracleConnection(connectionString),
command As New OracleCommand("UPDATE MyTable SET MyColumn = :MyColumn", connection)
command.Parameters.Add("MyColumn", OracleType.Blob, -1).Value = data
connection.Open()
command.ExecuteNonQuery()
End Using
我不使用Oracle,因此在这方面我不确定它是否与SQL Server完全相同,但是Add
的第三个参数是列的大小,而-1用于{ {1}}。这可能适用于MAX
列,也可能不适用于该列,因此请忽略该参数,如果适用则将其设置为适当的值。