我在Oracle 11g(11.2.0.1.0)中有这个表“T”。
CD NVARCHAR2(20) Primary Key
,VAL NUMBER(1)
更新时,它会抛出ORA-1722。
_factory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client")
...(Createconnection, CreateAdapter,CreateCommand, Transaction)
_command.CommandText="UPDATE T SET VAL = :VAL WHERE CD = :CD"
dim p1 as DbParameter = _command.CreateParameter()
p1.DbType = DbType.String
p1.Value = "AX-0001"
p1.ParameterName = "CD"
_command.Parameters.Add(p1)
dim p2 as DbParameter = _command.CreateParameter()
p2.DbType = DbType.Decimal
p2.Value = 3
p2.ParameterName = "VAL"
_command.Parameters.Add(p2)
_command.ExecuteNonQuery() ' ->throw exception
但是,Select查询返回了正确的DataTable。
_factory = DbProviderFactories.GetFactory("Oracle.DataAccess.Client")
...(Createconnection, CreateAdapter,CreateCommand, Transaction)
_command.CommandText="SELECT CD, VAL FROM T WHERE CD = :CD AND VAL = :VAL"
dim p1 as DbParameter = _command.CreateParameter()
p1.DbType = DbType.String
p1.Value = "AX-0001"
p1.ParameterName = "CD"
_command.Parameters.Add(p1)
dim p2 as DbParameter = _command.CreateParameter()
p2.DbType = DbType.Decimal
p2.Value = 3
p2.ParameterName = "VAL"
_command.Parameters.Add(p2)
_adapter.SelectCommand = _command
_command.ExecuteNonQuery() ' ->success
我错了什么?
我尝试将CreateParameter修改为New OracleParameter,将DbType修改为OracleDbType,但它也会抛出ORA-1722。
添加更多...(1/20 02:04 GMT)
我通过我的函数设置了更多参数“Size”,“Precision”,“Scale”,但它还没有解决抛出ORA-1722。
AddDecimalParam("VAL", 3, 1,0)
Public Overridable Sub AddDecimalParam(name As String, value As Object, precision As Byte, scale As Byte)
Dim p As DbParameter = _command.CreateParameter()
p.ParameterName = name
p.DbType = DbType.Decimal
p.Value = value
p.Size = precision + scale
p.Precision = precision ' -> can't change value! keep "0".
p.Scale = scale
p.Direction = ParameterDirection.Input
_command.Parameters.Add(p)
End Sub
通过这种方式,我得到了相同的结果。
AddOraDecimalParam("HANBAISAKI_MONEY", row("HANBAISAKI_MONEY"), 1, 0)
Public Overridable Sub AddOraDecimalParam(name As String, value As Object, precision As Byte, scale As Byte)
Dim p As Oracle.DataAccess.Client.OracleParameter = New OracleParameter(name, OracleDbType.Decimal, precision + scale, ParameterDirection.Input, True, precision, scale, name, DataRowVersion.Proposed, Nothing)
p.Value = value
_command.Parameters.Add(p)
End Sub
答案 0 :(得分:0)
我解决了这个问题。 ODP.NET默认使用decralation的参数顺序而不是name。 我必须设置“command.BindByName”为真。
我修改了我的DbUtility。
Protected _factory As System.Data.Common.DbProviderFactory
Protected _connection As DbConnection = Nothing
Protected _adapter As DbDataAdapter = Nothing
Protected _command As DbCommand = Nothing
Public Sub New(target as String)
_factory = System.Data.Common.DbProviderFactories.GetFactory(target)
_connection = _factory.CreateConnection()
_connection.ConnectionString = DbConfig.getIns().ConnectionString
_connection.Open()
_adapter = _factory.CreateDataAdapter
_command = _connection.CreateCommand
_command.CommandType = CommandType.Text
If TypeOf _command Is OracleCommand Then
DirectCast(_command, OracleCommand).BindByName = True
End If
End Sub
谢谢。