0

我想从组合框中读取数据。我知道如何在 MySQL 中做到这一点,它看起来像这样:

Private Sub cmbDescrip_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbDescrip.SelectedIndexChanged

 Dim getCategoryDesc As MySqlCommand = New MySqlCommand("Select * from category where catDesc=@field2;", connection)

    Dim reader1 As MySqlDataReader

    If connection.State = ConnectionState.Closed Then
        connection.Open()
    End If

    Try
        With getCategoryDesc
            .Parameters.AddWithValue("@field2", cmbDescrip.Text)
        End With

        reader1 = getCategoryDesc.ExecuteReader()

        While (reader1.Read())
            txtcatNo.Text = (reader1("catNo"))
        End While

        getCategoryDesc.Dispose()
        reader1.Close()

    Catch ex As Exception

    End Try

End sub

是的,该代码有效,但现在我正在使用 sql server 2012,这是一个我不太熟悉的数据库。问题是 sql server 似乎没有读取“@field2”。在 MySQL 中确实如此,这就是我的问题所在。那么如何让它与 sql server 一起工作呢?

4

1 回答 1

2

您只需要将 MYSQL 命令更改为 SQL 命令...如下所示...

    Dim getCategoryDesc As SqlCommand = New SqlCommand("Select * from category where catDesc=@field2;", connection)

    Dim reader1 As SqlDataReader

    If connection.State = ConnectionState.Closed Then
        connection.Open()
    End If

    Try
        With getCategoryDesc
            .Parameters.AddWithValue("@field2", cmbDescrip.Text)
        End With

        reader1 = getCategoryDesc.ExecuteReader()

        While (reader1.Read())
            txtcatNo.Text = (reader1("catNo"))
        End While

        getCategoryDesc.Dispose()
        reader1.Close()

    Catch ex As Exception

    End Try
于 2013-09-03T15:31:37.113 回答