-1

我有 vb.net 应用程序表单,它包含 id 作为组合框。单击编辑按钮并选择其他ID后,我发现错误如下图所示。那该怎么办?

Private Sub searchparfum()
    Dim dt As New DataTable
    Dim ds As New DataSet
    ds.Tables.Add(dt)
    Dim da As New OleDbDataAdapter("select num from parfum", MaConnection)
    da.Fill(dt)
    Dim r As DataRow
    cmb_parfum.AutoCompleteCustomSource.Clear()
    For Each r In dt.Rows
        cmb_parfum.AutoCompleteCustomSource.Add(r.Item(0).ToString)
    Next
    cmb_parfum.AutoCompleteMode = AutoCompleteMode.SuggestAppend
    cmb_parfum.AutoCompleteSource = AutoCompleteSource.CustomSource
End Sub

Private Sub cmb_parfum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_parfum.SelectedIndexChanged
    qry = "select * from parfum where num ='" & (cmb_parfum.Text) & "' "
    cmd = New OleDbCommand(qry, MaConnection)
    cmd.ExecuteNonQuery()
    dr = cmd.ExecuteReader
    If dr.Read Then

        txt_num.Text = dr("num")
        lab_nom.Text = dr("nom")
        lab_prix.Text = dr("prix")
        stock_par.Text = dr("stock")
        txt_ventes.Text = dr("ventes")
        photo.Text = dr("photo")

    End If
End Sub

在此处输入图像描述

4

2 回答 2

0

正如错误消息所说,您没有与数据库的开放连接。添加一个 open 和 close 并可能将其包装在 Try .. Catch 中,如下所示:

    cmd = New OleDbCommand(qry, MaConnection)
    Try
        MaConnection.Open()
        cmd.ExecuteNonQuery()
        dr = cmd.ExecuteReader
        If dr.Read Then
            txt_num.Text = dr("num")
            lab_nom.Text = dr("nom")
            lab_prix.Text = dr("prix")
            stock_par.Text = dr("stock")
            txt_ventes.Text = dr("ventes")
            photo.Text = dr("photo")
        End If
    Catch ex As Exception
        ' Handle any exception
    Finally
        MaConnection.Close()
    End Try

还要考虑在 SQL 命令中使用参数

于 2021-01-31T09:39:33.937 回答
0

命令和连接之类的数据库对象应在使用它们的方法中声明,以便正确处理它们。Using...End Using即使有错误,块也会为您执行此操作。

你不会使用cmd.ExecuteNonQuery()forSelect声明。仅用于Update, InsertorDelete命令。

您不希望在更新用户界面时连接保持打开状态。因此,我们加载了一个数据表,它在使用数据时不需要连接保持打开状态。阅读器确实需要一个开放的连接。

photo 真的是字符串类型吗?

Private ConStr As String = "Your connection string"

Private Sub cmb_parfum_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb_parfum.SelectedIndexChanged
    Dim dt As New DataTable
    Dim qry = "select * from parfum where num = @Parfum;"
    Using cn As New OleDbConnection(ConStr),
            cmd As New OleDbCommand(qry, cn)
        cmd.Parameters.Add("@Parfum", OleDbType.VarWChar).Value = cmb_parfum.Text
        cn.Open()
        Using dr = cmd.ExecuteReader
            dt.Load(dr)
        End Using
    End Using 'connection is closed and disposed and command is disposed
    If dt.Rows.Count > 0 Then
        txt_num.Text = dt(0)("num").ToString
        lab_nom.Text = dt(0)("nom").ToString
        lab_prix.Text = dt(0)("prix").ToString
        stock_par.Text = dt(0)("stock").ToString
        txt_ventes.Text = dt(0)("ventes").ToString
        photo.Text = dt(0)("photo").ToString
    End If
End Sub
于 2021-02-01T01:43:26.450 回答