【问题标题】:SQL parameter either integer value or dbnull pass to querySQL 参数可以是整数值或 dbnull 传递给查询
【发布时间】:2016-05-02 10:17:18
【问题描述】:

无法让我的查询工作。我想将整数值或 dbnull 传递给我的查询,但这种形式不起作用。在我的代码下方。

查询:

Using cmd As New SqlCommand("SELECT COUNT(*) FROM tbSuSubSections_Sentences WHERE FK_Sentence_Id = @FK_Sentence_Id And FK_SubSection_Id = @FK_SubSection_Id And FK_SubSubKategorie_Id=@FK_SubSubKategorie_Id", con)

根据我需要的参数设置:

If _FK_SubSubKategorie_Id = 0 Then
                    cmd.Parameters.AddWithValue("@FK_SubSubKategorie_Id", DBNull.Value)
                Else
                    cmd.Parameters.AddWithValue("@FK_SubSubKategorie_Id", _FK_SubSubKategorie_Id)
                End If

如果_FK_SubSubKategorie_Id 设置为DBNull,如图所示,我的查询应该有所改变,而不是这部分:

And FK_SubSubKategorie_Id=@FK_SubSubKategorie_Id", con)

And FK_SubSubKategorie_Id IS NULL", con)

正确的做法是什么?

这就是整个 SQL 函数:

#Region "Check if connection already exist"
    Public Function CheckIfConnectionAlreadyExist(_FK_Sentence_Id As Integer, _FK_SubSection_Id As Integer, _FK_SubSubKategorie_Id As Integer) As Boolean
        Dim result As Boolean
        Dim strcon = New AppSettingsReader().GetValue("ConnectionString", GetType(String)).ToString()
        Using con As New SqlConnection(strcon)
            Using cmd As New SqlCommand("SELECT COUNT(*) FROM tbSuSubSections_Sentences WHERE FK_Sentence_Id = @FK_Sentence_Id And FK_SubSection_Id = @FK_SubSection_Id And FK_SubSubKategorie_Id=@FK_SubSubKategorie_Id", con)
                cmd.CommandType = CommandType.Text

                cmd.Parameters.AddWithValue("@FK_Sentence_Id", _FK_Sentence_Id)
                cmd.Parameters.AddWithValue("@FK_SubSection_Id", _FK_SubSection_Id)
                If _FK_SubSubKategorie_Id = 0 Then
                    cmd.Parameters.AddWithValue("@FK_SubSubKategorie_Id", DBNull.Value)
                Else
                    cmd.Parameters.AddWithValue("@FK_SubSubKategorie_Id", _FK_SubSubKategorie_Id)
                End If
                con.Open()
                Dim o As Integer = cmd.ExecuteScalar()
                If o > 0 Then
                    result = True
                Else
                    result = False
                End If
                con.Close()
            End Using
        End Using
        Return result
    End Function
#End Region

【问题讨论】:

  • 既然您已经在代码中直接将查询编写为字符串,您就不能简单地相应地更改该字符串吗?
  • 你能告诉我怎么做吗?我的意思是正确的方式......
  • 我认为传递 Null 而不是 DBNull.Value 会成功。
  • ...或者甚至更好地使用 Nullable Integer for _FK_SubSubKategorie_Id

标签: vb.net


【解决方案1】:

只需创建一个两个条件共有的查询字符串,然后将其更改为_FK_SubSubKategorie_Id。然后创建命令:

Dim qry as String = "SELECT COUNT(*) FROM tbSuSubSections_Sentences WHERE FK_Sentence_Id = @FK_Sentence_Id And FK_SubSection_Id = @FK_SubSection_Id And FK_SubSubKategorie_Id {0}"

If _FK_SubSubKategorie_Id = 0 Then
  qry = string.Format(qry," IS NULL")
Else
  qry = string.Format(qry, "=@FK_SubSubKategorie_Id"
End if

Using cmd As New SqlCommand(qry, con)
  ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 2015-04-25
    • 1970-01-01
    • 2010-10-10
    • 1970-01-01
    相关资源
    最近更新 更多