AddWithValue()不接受DBNull.Value(异常0x800A0BB9) - Silverlight - VB

时间:2014-07-18 19:30:56

标签: sql vb.net silverlight parameters ado

我正在开发一个OOB Silverlight应用程序并使用COM Toolkit(http://silverlightcom.codeplex.com),该语言是Visual Basic(VB)。有了这个包,我可以使用ADO.NET将我的应用程序与我的Microsoft SQL Server数据库连接。它可以工作,但是当我尝试向参数插入一个空值时,它会抛出一个异常:0x800A0BB9(http://prntscr.com/43zwyr)。

我的代码是(我将发布最简单的内容):

    Private Const SqlCreateJob As String = "insert into Cargos(Nome_carg, Desc_carg) values(?, ?)"

    Public Shared Sub CreateJob(ByVal j As Job)

        Dim conn As AdoConnection = DbUtil.GetConn
        Dim cmd As AdoCommand = conn.CreateCommand
        cmd.CommandText = SqlCreateJob

        cmd.Parameters.AddWithValue("Nome_carg", j.Name)
        cmd.Parameters.AddWithValue("Desc_carg", j.Desc)

        For Each p As AdoParameter In cmd.Parameters
            If p.Value = Nothing Then
                p.Value = DBNull.Value
            End If
        Next

        cmd.ExecuteNonQuery()
        conn.Close()

    End Sub

班级工作:

Public Class Job
    Property Code() As Integer
    Property Name() As String
    Property Desc() As String
End Class

来自Job的Desc值可以为null。如果我放 cmd.Parameters.AddWithValue("Desc_carg", DBNull.Value)它仍然无法运作。我能做什么? (如果Desc不为null,则命令有效)

Exception.ToString在这里:

System.Runtime.InteropServices.COMException (0x800A0BB9): Exception from HRESULT: 0x800A0BB9 ---> MS.Internal.ComAutomation.ComAutomationObjectException: The arguments are incorrect, are out of acceptable range, or are in conflict.(Source=ADODB.Parameter) (HelpFile=C:\WINDOWS\HELP\ADO270.CHM#1240641) em MS.Internal.ComAutomation.ComAutomationNative.CheckInvokeHResult(UInt32 hr, String memberName, String exceptionSource, String exceptionDescription, String exceptionHelpFile, UInt32 exceptionHelpContext) em MS.Internal.ComAutomation.ComAutomationNative.Invoke(Boolean tryInvoke, String memberName, ComAutomationInvokeType invokeType, ComAutomationInteropValue[] rgParams, IntPtr nativePeer, ComAutomationInteropValue& returnValue) em MS.Internal.ComAutomation.ComAutomationObject.InvokeImpl(Boolean tryInvoke, String name, ComAutomationInvokeType invokeType, Object& returnValue, Object[] args) em MS.Internal.ComAutomation.ComAutomationObject.Invoke(String name, ComAutomationInvokeType invokeType, Object[] args) em System.Runtime.InteropServices.Automation.AutomationMetaObjectProvider.TrySetMember(SetMemberBinder binder, Object value) em System.Runtime.InteropServices.Automation.AutomationMetaObjectProviderBase.<.cctor>b__3(Object obj, SetMemberBinder binder, Object value) em CallSite.Target(Closure , CallSite , Object , Int32 ) em System.Dynamic.UpdateDelegates.UpdateAndExecute2[T0,T1,TRet](CallSite site, T0 arg0, T1 arg1) em CallSite.Target(Closure , CallSite , Object , Int32 ) em ComToolkit.Data.AdoParameter.set_Type(DbType value) em ComToolkit.Data.AdoParameter..ctor(Object adoParameter, String name, Object value) em CallSite.Target(Closure , CallSite , Type , Object , String , Object ) em System.Dynamic.UpdateDelegates.UpdateAndExecute4[T0,T1,T2,T3,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2, T3 arg3) em CallSite.Target(Closure , CallSite , Type , Object , String , Object ) em ComToolkit.Data.AdoCommand.CreateParameter(String name, Object value) em ComToolkit.Data.AdoParameterCollection.AddWithValue(String name, Object value) em Corporativo.Dao.CargoDao.AddParameters(AdoCommand cmd, Cargo c) em Corporativo.Dao.CargoDao.CriarCargo(Cargo c) em Corporativo.UCCadCargos.Create()    

和.Message:

  

HRESULT的异常:0x800A0BB9

1 个答案:

答案 0 :(得分:1)

如果值为或者为van,则不应使用AddWithValueAddWithValue从值中推断出参数的数据类型,但是如果值为NULL,则无法推断出类型。在这种情况下,您应该使用Add,明确指定数据类型并设置Value属性。