Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server ExecuteSQLCommand将布尔值从False更改为NULL_Sql Server_Vb.net - Fatal编程技术网

Sql server ExecuteSQLCommand将布尔值从False更改为NULL

Sql server ExecuteSQLCommand将布尔值从False更改为NULL,sql-server,vb.net,Sql Server,Vb.net,我有一个WebInvoke方法: <OperationContract()> <WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json)> Public Function Actions_Update_ByID(ByVal aID As Int32, Optional aActionID As Int32 = Nothing, Optional aConsequence_Paid As B

我有一个WebInvoke方法:

  <OperationContract()>
  <WebInvoke(Method:="POST", ResponseFormat:=WebMessageFormat.Json)>
  Public Function Actions_Update_ByID(ByVal aID As Int32, Optional aActionID As Int32 = Nothing, Optional aConsequence_Paid As Boolean = Nothing, Optional aDate As DateTime = Nothing, Optional aName As String = Nothing, Optional aNotes As String = Nothing, Optional aReward_Paid As Boolean = Nothing) As String
    Try
      Dim c As New Common
      Dim ps As SqlParameter() = {New SqlParameter("@ID", SqlDbType.Int), New SqlParameter("@ActionID", SqlDbType.Int), New SqlParameter("@Consequence_Paid", SqlDbType.Bit), New SqlParameter("@Date", SqlDbType.DateTime), New SqlParameter("@Name", SqlDbType.NVarChar), New SqlParameter("@Notes", SqlDbType.NVarChar), New SqlParameter("@Reward_Paid", SqlDbType.Bit)}
      ps(0).Value = aID
      ps(1).Value = IIf(aActionID = Nothing, DBNull.Value, aActionID)
      ps(2).Value = IIf(aConsequence_Paid = Nothing, DBNull.Value, aConsequence_Paid)
      ps(3).Value = IIf(aDate = Nothing, DBNull.Value, aDate)
      ps(4).Value = IIf(aName = Nothing, DBNull.Value, aName)
      ps(5).Value = IIf(aNotes = Nothing, DBNull.Value, aNotes)
      ps(6).Value = IIf(aReward_Paid = Nothing, DBNull.Value, aReward_Paid)
      c.ExecuteSQLCommand("UPDATE [Actions] SET [ActionID] = @ActionID,[Consequence_Paid] = @Consequence_Paid,[Date] = @Date,[Name] = @Name,[Notes] = @Notes,[Reward_Paid] = @Reward_Paid WHERE [ID] = @ID", ps)
      Return (New JavaScriptSerializer()).Serialize(String.Empty)
    Catch ex As Exception
      Dim row As New Dictionary(Of String, String) From {{"error", ex.Message}}
      Return (New JavaScriptSerializer()).Serialize(row)
    End Try
  End Function
此处的ExecuteSQLCommand:

  Sub ExecuteSQLCommand(ByVal commandText As String, ByVal params As SqlParameter())
    Dim con As New SqlConnection(GetConnectionString())
    Try
      con.Open()

      Dim com As New SqlCommand(commandText, con)
      If Not params Is Nothing Then
        If params.Count > 0 Then
          com.Parameters.AddRange(params)
        End If
      End If
      com.ExecuteNonQuery()

    Finally
      If con.State = ConnectionState.Open Then
        con.Close()
      End If
    End Try
  End Sub
所以,每次我向布尔参数发送False(aConsequence\u Paid,aReward\u Paid)时,我在数据库中得到Null值,如何

谢谢, 德扬

  • 可选基本参数的默认值永远不能是
    Nothing
    。通过指定
    Nothing
    ,未提供的任何参数将默认为各自的默认值,因此
    aConsequence\u Paid
    将为false
  • Iif
    比较将是错误的比较,因此
    false=Nothing
    将计算为true
  • 因此,如果没有提供值,或者提供了false,那么DbNull将被传递给查询
  • 我建议使用可空类型

    Public Function Actions_Update_ByID(Optional aConsequence_Paid = As Boolean? = Nothing)
      ' ...
      ps(2).Value = IIf(aConsequence_Paid.HasValue, aConsequence_Paid.Value, DBNull.Value)
      ' ...
    End Function
    
  • 可选基本参数的默认值永远不能是
    Nothing
    。通过指定
    Nothing
    ,未提供的任何参数将默认为各自的默认值,因此
    aConsequence\u Paid
    将为false
  • Iif
    比较将是错误的比较,因此
    false=Nothing
    将计算为true
  • 因此,如果没有提供值,或者提供了false,那么DbNull将被传递给查询
  • 我建议使用可空类型

    Public Function Actions_Update_ByID(Optional aConsequence_Paid = As Boolean? = Nothing)
      ' ...
      ps(2).Value = IIf(aConsequence_Paid.HasValue, aConsequence_Paid.Value, DBNull.Value)
      ' ...
    End Function