简体   繁体   中英

How to display in listview columns from different tables

I have a table which has foreign key from another table with information; it's like: in table person I have column country_id from table country where I keep some countries; and I want to display information from different columns of table person in listviews and to display country name from table country not the country_id which is in table person . This is my code:

        nameview.Items.Clear();
        countryview.Items.Clear();
        emailview.Items.Clear();
        bankview.Items.Clear();
        cardnrview.Items.Clear();
        com.CommandText = "Select * from Account";
        dataSearch = com.ExecuteReader();
        if (dataSearch.HasRows)
        {
            while (dataSearch.Read())
            {
                nameview.Items.Add(dataSearch["NickName"].ToString());
                countryview.Items.Add(dataSearch["Country_ID"].ToString());
                emailview.Items.Add(dataSearch["Email"].ToString());
                bankview.Items.Add(dataSearch["BankAccount_ID"].ToString());
                cardnrview.Items.Add(dataSearch["Card_Number"].ToString());
            }
        }

Please type complete query in com.CommandText and fetch the keys accordingly.

Instead of this: com.CommandText = "Select * from Account" ;

Try this:

com.CommandText = "Select a.bankAccountId as 'BankAccount_ID', a.cardNumber 
as 'Card_Number', p.nickName as 'NickName', c.countryName as 'Country_Name' 
from Account A inner join Person P on A.accountId = P.accountId
inner join country c on c.countryId = p.countryId";

Now you should be able to do this:

if (dataSearch.HasRows)
    {
        while (dataSearch.Read())
        {
            nameview.Items.Add(dataSearch["NickName"].ToString());
            .
            .
            .
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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