简体   繁体   English

使用内部联接时,列表框内容为空

[英]listbox contents empty when using inner join

I am trying to populate a listbox with contents from 2 tables. 我正在尝试使用2个表中的内容填充列表框。 When I try to "select all" from either table, I get results in my listbox. 当我尝试从任一表中“全选”时,我在列表框中得到了结果。 When I try to use the innerjoin query, the listbox is empty. 当我尝试使用innerjoin查询时,列表框为空。 No errors, just an empty box. 没有错误,只是一个空盒子。 The fields to join are customers.custID=sales.cust_id . 要加入的字段是customer.custID = sales.cust_id。 What have I done wrong? 我做错了什么?

cust_cn.ConnectionString = "Data Source=localhost;Initial Catalog=tires;Integrated Security=True"
    cust_cn.Open()
    'Dim selectStatement As String = "SELECT * FROM customers as C inner join sales as S on c.custID=s.cust_ID "
    'Dim b As New System.Text.StringBuilder()
    'Dim adpt As New SqlDataAdapter(selectStatement, cust_cn)
    'Dim myDataSet As New DataSet()
    'adpt.Fill(myDataSet)
    'Dim myDataTable As DataTable = myDataSet.Tables(0)
    'Dim tempRow As DataRow
    'For Each tempRow In myDataTable.Rows
    'lstCustSales.Items.Add(tempRow(tempRow("c.first") & tempRow("c.last") & "TIRE ID: " & tempRow("s.tire_id") & "QTY: " & tempRow("s.qty") & tempRow("s.notes")))
    'Next


    'Dim selectStatement As String = "select * from customers"
    'Dim b As New System.Text.StringBuilder()
    'Dim adpt As New SqlDataAdapter(selectStatement, cust_cn)
    'Dim myDataSet As New DataSet()
    'adpt.Fill(myDataSet, "customer_data")
    'Dim myDataTable As DataTable = myDataSet.Tables(0)
    'Dim tempRow As DataRow
    'For Each tempRow In myDataTable.Rows
    '    lstCustSales.Items.Add(tempRow("custphone") & "   " & tempRow("last") & tempRow("first") & " " & tempRow("address") & "   " & tempRow("zip") & temprow("custid"))
    'Next

    Dim selectStatement As String = "select * from sales"
    Dim b As New System.Text.StringBuilder()
    Dim adpt As New SqlDataAdapter(selectStatement, cust_cn)
    Dim myDataSet As New DataSet()
    adpt.Fill(myDataSet, "sales_data")
    Dim myDataTable As DataTable = myDataSet.Tables(0)
    Dim tempRow As DataRow
    For Each tempRow In myDataTable.Rows
        lstCustSales.Items.Add(tempRow("date") & "   " & tempRow("cust_id") & tempRow("tire_id") & " " & tempRow("qty") & "   " & tempRow("total") & tempRow("notes"))
    Next

when I go to management studio and type this: 当我去Management Studio并输入以下内容时:

SELECT * FROM customers as C inner join sales as S on c.custID=s.cust_ID where c.custphone=7146511734

I get the full results from both tables, limited to my phone# (correct/intended results) 我从两个表中获得了全部结果,但仅限于我的电话号码(正确/预期的结果)

I haven't played with VB datasets for a very long time, there is a difference between how a client side dataset resolves each of the column names, versus how it is done in management studio. 我已经很长时间没有使用VB数据集了,客户端数据集如何解析每个列名与在Management Studio中如何完成之间是有区别的。 Essentially in Management Studio I am assuming that it is purely a string output, and no referencing to the column name is required. 从本质上讲,在Management Studio中,我假定它纯粹是字符串输出,并且不需要引用列名。 This could cause an issue if there are fields in both tables with the same name. 如果两个表中的字段名称相同,则可能导致问题。

I suggest being explicit in your select statement about the fields you actually want retrieved from the query, instead of using " SELECT * ". 我建议您在select语句中明确声明您实际上要从查询中检索到的字段,而不要使用“ SELECT * ”。 This is good practice anyway as returning additional fields is a waste of resources, especially if there are BLOB fields 无论如何,这是一个好习惯,因为返回额外的字段会浪费资源,尤其是在存在BLOB字段的情况下

SELECT c.first, c.last, s.tire_id, s.qty, s.notes

Actually looking at your code again - you have a tempRow, looking for a column named tempRow(" ... ") 实际上再次查看您的代码-您有一个tempRow,正在寻找一个名为tempRow(“ ...”)的列

lstCustSales.Items.Add(tempRow(tempRow("c.first") & tempRow("c.last") & "TIRE ID: " & tempRow("s.tire_id") & "QTY: " & tempRow("s.qty") & tempRow("s.notes")))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM