0

我有一个包含 6 个组框的表单,每个组框包含 180 个文本框,以及两个组合框。从第一个组合框中选择一个值后,第二个组合框将填充表中所需的数据。我的要求是,从第二个组合框中选择一个值后,来自同一个表的过滤数据应该填充剩余的文本框。我正在使用的代码如下:

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox2.SelectedIndexChanged
    Dim strConnection As String = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=c:\\users\\brisingr\\documents\\123\database.mdb"
    Dim objConnection As New OleDbConnection(strConnection)

    Dim strsql As String
    'strsql = "Select * from '" & ComboBox1.Text & "' where Style = '" & ComboBox2.Text & " '"
    Dim a As String
    Dim b As String
    Dim c As String
    Dim d As String
    a = "Select * from ["
    b = ComboBox1.Text
    c = "] where Style = ["
    d = ComboBox2.Text
    strsql = a & b & c & d & "]"
    Dim objCommand As New OleDbCommand(strsql, objConnection)

    Dim objDataAdapter As New OleDbDataAdapter(objCommand)
    Dim objDataTable As New DataTable("Buyers")
    objDataAdapter.Fill(objDataTable)

    objConnection.Close()
    objConnection.Dispose()
    objConnection = Nothing
    objCommand.Dispose()
    objCommand = Nothing
    objDataAdapter.Dispose()
    objDataAdapter = Nothing

    For i As Integer = 1 To 60
        Me.Controls("L1Ob" & i).Text = objDataTable.Rows(0)("Operation" & i)
    Next

End Sub

这里的“Operation” & (i) 指的是数据库的字段名,已经命名为 operation1、operation2 等等……

这段代码似乎对我不起作用。请帮忙..

4

1 回答 1

1

这里我们有很多问题

Dim a As String
Dim b As String
Dim c As String
Dim d As String
a = "Select * from ["
b = ComboBox1.Text
c = "] where Style = ["
d = ComboBox2.Text
strsql = a & b & c & d & "]"
Dim objCommand As New OleDbCommand(strsql, objConnection)

首先,为 field 传递的值周围有方括号Style
这不是一个有效的语法。Jet 引擎假定这是一个参数。实际上,传递参数是正确的方法。

a = "Select * from [" 
b = ComboBox1.Text
c = "] where Style = ?"
strsql = a & b & c
Dim objCommand As New OleDbCommand(strsql, objConnection)
objCommand.Parameters.AddWithValue("@p1", ComboBox2.Text)
Dim objDataAdapter As New OleDbDataAdapter(objCommand)
Dim objDataTable As New DataTable("Buyers")
objDataAdapter.Fill(objDataTable)

该字段的值Style通过参数传递。这留下了用从组合框获取到框架的值替换占位符 (?) 的工作。
您可以确定它知道更好地避免错误和 sql 注入。

这段代码仍然很弱。您应该绝对确定第一个 combobox1 的内容不能被修改为包含恶意文本而不是表名

于 2013-04-20T10:57:58.787 回答