如何从组合框中读取数据

时间:2013-09-03 15:03:02

标签: sql-server vb.net combobox

我想从组合框中读取数据。我知道如何在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一起使用?

1 个答案:

答案 0 :(得分: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