当我从组合框中选择名称时,如何显示3个表中的数据。我假设我需要加入有问题的3个表,但我不确定语法。
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
string constring = ("Data Source=****************");
string Query = "select * from Table1 where Last='" + cbUsers.SelectedItem.ToString() + "' ;";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sFirst = myReader.GetString(2); //From Table1
string sLast = myReader.GetString(1); //From Table1
string sdob = myReader.GetString(3);//From Table2
string scode = myReader.GetString(1);//From Table3
txtFirst.Text = sFirst;
txtdob.Text = sdob;
txtcode.Text = scode;
答案 0 :(得分:1)
遗传密码是
select
table1.sFirst,
table1.sLast,
table2.sdob,
table3.scode
from table1
inner join table2 on table1.keycol=table2.keycol
inner join table3 on table2.keycol=table3.keycol
where table1.Last='some value'
答案 1 :(得分:0)
因为我的意思是你忘了只加入3个表,所以用这种方式改变代码:
private void ComboBox_SelectionChanged(object sender,SelectionChangedEventArgs e) {
string constring = ("Data Source=****************");
string Query = "select t1.sFirst, t1.Last, t2.sdob, t3.scode from Table1 as t1 Table2 as t2, table 3 as t3 where t1.Last='" + cbUsers.SelectedItem.ToString() + "' AND t1.key = t2.key AND t2.key = t3.key";
SqlConnection conDataBase = new SqlConnection(constring);
SqlCommand cmdDataBase = new SqlCommand(Query, conDataBase);
SqlDataReader myReader;
try
{
conDataBase.Open();
myReader = cmdDataBase.ExecuteReader();
while (myReader.Read())
{
string sFirst = myReader.GetString(1); //From Table1
string sLast = myReader.GetString(2); //From Table1
string sdob = myReader.GetString(3);//From Table2
string scode = myReader.GetString(4);//From Table3
txtFirst.Text = sFirst;
txtdob.Text = sdob;
txtcode.Text = scode;