VB.Net/Access SQL 插入子查询问题

Posted

技术标签:

【中文标题】VB.Net/Access SQL 插入子查询问题【英文标题】:VB.Net/Access SQL insertion sub query issue 【发布时间】:2018-06-07 18:33:33 【问题描述】:

我正在制作一个可以填写的表格,并将执行一个带有按钮的插入查询。我不知道如何在这段代码中引用另一个表。我正在尝试在表单中插入姓名、电话号码和电子邮件以及相应的分销商 ID。获取 Distributor ID 的唯一方法是引用 Distributor 表。有人有想法么?谢谢!

Private Sub Button1_Click(sender As Object, e As EventArgs) 处理 Button1.Click

    provider = 0
    dataFile = 0
    provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    dataFile = "XXXXXXX.mdb"
    connString = provider & dataFile
    myConnection.ConnectionString = connString
    myConnection.Open()

    '*THIS IS THE PROBLEM AREA*'
    Dim str As String
    str = "INSERT INTO [Distributor Contact] ([Name], [Phone Number], [Email], [Distributor ID]) VALUES(?,?,?,?) WHERE [Distributor ID] IN (SELECT DISTINCT [Distributor ID] FROM [Distributor] WHERE [Distributor Name]= '" & ComboBox_Dist.SelectedItem.ToString() & "'))"

    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    cmd.Parameters.Add(New OleDbParameter("Name", CType(TextBox2.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Phone Number", CType(TextBox3.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Email", CType(TextBox4.Text, String)))
    Me.Refresh()

    Try
        cmd.ExecuteNonQuery()
        cmd.Dispose()
        myConnection.Close()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()

        MsgBox("Contact Added")
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
    myConnection.Close()

    'RE-POPULATE COMBOBOX
    ComboBox_Dist.Items.Clear()
    Dim connString1 As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= XXXXXXXXX.mdb"
    Dim con As OleDbConnection = New OleDbConnection(connString1)
    Dim adapter As OleDbDataAdapter
    Dim cmd2 As OleDbCommand
    Dim dt As DataTable = New DataTable()

    Dim sql As String = "SELECT * FROM [Distributor Contact];"

    cmd2 = New OleDbCommand(sql, con)

    Try
        con.Open()
        adapter = New OleDbDataAdapter(cmd2)

        adapter.Fill(dt)

        'Add Items To ComboBox
        For Each row In dt.Rows
            ComboBox_Dist.Items.Add(row(1))
        Next
        con.Close()
    Catch ex As Exception
        MsgBox(ex.Message)
        con.Close()
    End Try
End Sub

【问题讨论】:

【参考方案1】:

添加 ComboBox_Dist 作为参数,然后使用 Select 而不是 Values 进行插入查询。 select 允许您引用 Distributor 表。

cmd.Parameters.Add(New OleDbParameter("Email", CType(TextBox4.Text, String)))
cmd.Parameters.Add(New OleDbParameter("DistributorName", ComboBox_Dist.SelectedItem.ToString()))

str = "INSERT INTO [Distributor Contact] ([Name], [Phone Number], [Email], [Distributor ID]) "
str += "SELECT ?,?,?, [Distributor].[DistributorID] "
str += "FROM [Distributor] WHERE [Distributor].[Distributor Name] = ?"

【讨论】:

嗯....我进行了更改,现在在新的 ComboBox_Dist 参数行上出现错误。错误消息:“没有为一个或多个必需参数提供值。对象引用未设置为对象的实例。” 听起来 ComboBox_Dist.SelectedItem 为空/无。在运行该代码之前,您必须验证它是否具有值。 我能够通过 MsgBox 验证 ComboBox_Dist.SelectedItem.ToString() 是否正确保存了一个值。 其他参数你也保留了吗?我切断了前两个,因为我不想粘贴您的所有代码。 是的,我保留了它们。【参考方案2】:

这是未来任何人的最终工作代码。

    provider = 0
    dataFile = 0
    provider = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="
    dataFile = "XXXXXXXX.mdb"
    connString = provider & dataFile
    Dim str As String
    str = "INSERT INTO [Distributor Contact] ([Contact Name], [Phone Number], [Email], [Distributor ID])"
    str += "SELECT ?,?,?,[Distributor].[Distributor ID]"
    str += "FROM [Distributor] WHERE [Distributor].[Distributor Name] = ?"

    Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
    cmd.Parameters.Add(New OleDbParameter("Contact Name", CType(TextBox2.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Phone Number", CType(TextBox3.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Email", CType(TextBox4.Text, String)))
    cmd.Parameters.Add(New OleDbParameter("Distributor Name", ComboBox_Dist.SelectedItem.ToString()))

    Me.Refresh()

【讨论】:

以上是关于VB.Net/Access SQL 插入子查询问题的主要内容,如果未能解决你的问题,请参考以下文章

PL/SQL 在插入语句的子查询中使用别名和函数

如何使用 SQL 中的 Case 语句将数据插入临时表

关系数据库标准语言SQL02

通过 LEFT JOIN 优化 SQL 子查询

同时创建和插入表的SQL查询错误

如何使用具有多个结果的子查询将值插入表中?