选择具有不同主键的类似记录

时间:2014-06-02 03:41:41

标签: vb.net

在我的VB.Net程序中,我试图选择具有相同值但具有不同主键和详细信息的条目。例如,在我的数据库中,我有以下记录:

___ PK_____LastName_____FirstName_____Details

001168 _____ Smith________John_________纽约 001479_____Smith________John_________London
001698_____Smith________John_________Paris

我为PK和LastName使用两个不同的/单独的组合框,所以我使用两个不同的代码(下面)填充它们:

'This is for the PK
conn.Open()
    Dim dryear As New SqlClient.SqlDataAdapter("SELECT distinct cltaccountnum FROM tblclientinfo", conn)
    Dim dtyear As New DataTable
    dryear.Fill(dtyear)
    Me.cmbAcctNo1.DisplayMember = "cltaccountnum"
    Me.cmbAcctNo1.DataSource = dtyear
    conn.Close()

 'And this is for the Names
 conn.Open()
    Dim dryear5 As New SqlClient.SqlDataAdapter("SELECT cltlastname FROM tblclientinfo order by cltlastname", conn)
    Dim dtyear5 As New DataTable
    dryear5.Fill(dtyear5)
    Me.cmbClientName1.DisplayMember = "cltlastname"
    Me.cmbClientName1.DataSource = dtyear5
    conn.Close()

我包含了这些代码,这样每当我从任一组合框中选择值时,其他值的变化(根据相应的记录):

    Private Sub ComboBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbAcctNo1.SelectedIndexChanged
    'connection string

    If cmbAcctNo1.Text <> "" Then
        conn = GetConnect()


        conn.Open()

        Dim com As New SqlClient.SqlCommand
        Dim dr As SqlClient.SqlDataReader
        Dim dt As New DataTable

        com.CommandText = "Select * from tblclientinfo where cltaccountnum = '" & cmbAcctNo1.Text & "'"
        com.Connection = conn
        dr = com.ExecuteReader
        dr.Read()
        cmbClientName1.Text = dr!cltlastname
        Label13.Text = dr!cltfirstname

        conn.Close()

        conn.Open()
        com.CommandText = "select * from tblpropertypayments where cltaccountnumb = '" & cmbAcctNo1.Text & "'"
        com.Connection = conn
        dr = com.ExecuteReader
        dr.Read()
        txtUnitCode1.Text = dr!pmtLocCode

        'txtUnitCode1.Text = dr!cltunitcode
        'cmbParticulars1.Text = 
        'txtPayee1.Text = 
        'txtAmount1.Text = 
        conn.Close()

    Else
        cmbClientName1.Text = ""
        Label13.Text = ""
        txtUnitCode1.Text = ""
    End If


End Sub

 Private Sub cmbClientName1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbClientName1.SelectedIndexChanged

    If cmbClientName1.Text <> "" Then
        'connection string
        conn = GetConnect()
        conn.Open()

        Dim com As New SqlClient.SqlCommand
        Dim dr As SqlClient.SqlDataReader
        Dim dt As New DataTable
        com.CommandText = "Select * from tblclientinfo where cltlastname = '" & cmbClientName1.Text & "'"
        com.Connection = conn
        dr = com.ExecuteReader
        dr.Read()
        cmbAcctNo1.Text = dr!cltaccountnum
        Label13.Text = dr!cltfirstname

        conn.Close()

        conn.Open()
        com.CommandText = "select * from tblpropertypayments where cltaccountnumb = '" & cmbAcctNo1.Text & "'"
        com.Connection = conn
        dr = com.ExecuteReader
        dr.Read()
        txtUnitCode1.Text = dr!pmtLocCode

        'cmbParticulars1.Text = 
        'txtPayee1.Text = 
        'txtAmount1.Text = 
        conn.Close()
    Else
        txtUnitCode1.Text = ""
        cmbAcctNo1.Text = ""
        Label13.Text = ""
    End If

End Sub

它完全没问题,所以除了我上面提到的那个之外,我没有任何其他问题。会发生什么事情,因为,让我们采取上面的例子,记录中有三个'史密斯约翰'出现,我认为组合框'SelectedIndexChanged事件中的'SELECT'混淆了,我的意思是,无论哪个'史密斯约翰'我选择,只出现第一个PK'001168'(尽管其他PK出现在组合框中用于PK)。有没有办法让它知道我没有在这里找到相同的名字(只是一个类似的名字,但有不同的细节和PK)?

0 个答案:

没有答案