从2个表格填充Listview

时间:2018-08-04 15:00:00

标签: sql vb.net

我有2个表:tblstudentbasicinfotblzipcode

  • tblstudentbasicinfo具有以下列
    • TagID
    • Surname
    • Firstname
    • Middlename
    • Course
    • Year
    • District
    • ZipCode
    • ContacactNo
  • tblzipcode具有以下列
    • ZipCode
    • Province
    • Municipality
    • District

当学生输入信息时,用户可以根据tblzipcode中的内容键入地区名称和该地区的邮政编码。

我的问题是当我尝试填充列表视图以显示带有列的学生基本信息时

  • TagID
  • Surname
  • Firstname
  • Middlename
  • Course
  • Year
  • ZipCode
  • Province
  • Municipality
  • District
  • ContactNo

数据不会显示。

这是我尝试过的SQL查询:

Dim sql As String = "Select TagID,Surname,Firstname,Middlename,Course,Year,ZipCode,Province,Municipality,District,Contactno from tblstudentbasicinfo AS t1 Inner Join tblzipcode AS t2 On t1.ZipCode = t2.ZipCode and t1.District = t2.District"

但是我得到了错误:

  

字段列表中的“邮政编码”列不明确。

im是sql和vb的新功能。有人可以告诉我该如何解决?预先谢谢你

这是我在vb上的代码:

Private Sub Populatelstbasicinfo(TagID As String, Surname As String, Firstname As String, Middlename As String, Course As String, Year As String, ZipCode As String, Province As String, Municipality As String, District As String, contactno As String)
Dim row As String() = New String() {TagID, Surname, Firstname, Middlename, Course, Year, ZipCode, Province, Municipality, District, contactno}


lstbasicinfo.Items.Add(item)
End Sub

Private Sub Retrieve()


    lstbasicinfo.Items.Clear()
    Dim sql As String = "Select TagID,Surname,Firstname,Middlename,Course,Year,t1.ZipCode,Province,Municipality,t2.Barangay,Contactno from tblstudentbasicinfo AS t1 Inner Join tblzipcode AS t2 On t1.ZipCode = t2.ZipCode and t1.Barangay = t2.Barangay"
    cmd = New OdbcCommand(sql, con)

    Try

        con.Open()
        adapter = New OdbcDataAdapter(cmd)
        adapter.Fill(dt)

        For Each row In dt.Rows
            Populatelstbasicinfo(row(0), row(1), row(2), row(3), row(4), row(5), row(6), row(7), row(8), row(9), row(10), row(11), row(12), row(13), row(14), row(15), row(16))
        Next

        con.Close()
        dt.Rows.Clear()
    Catch ex As Exception
        MsgBox(ex.Message)
        con.Close()
    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

编写查询时,您应该学习良好的做法。因此,请考虑以下问题:

Select bi.TagID, bi.Surname, bi.Firstname, bi.Middlename, bi.Course, bi.Year,
       z.ZipCode, z.Province, z.Municipality, z.District, bi.Contactno
from tblstudentbasicinfo bi Inner Join
     tblzipcode z
     on bi.ZipCode = z.ZipCode and bi.District = z.District;

注意:

  • 所有列都是合格的,因此您知道它们来自什么表。
  • 表具有别名,因此查询更易于编写和阅读。
  • 空格和一致的大写字母增强了基本可读性。