连接到两个表

时间:2014-03-25 18:11:27

标签: c# .net combobox

我创建了comboBox并填充了一列,在我从组合框中选择项目之后我想在文本框中显示其他列,所以我编写了代码来实现它但是如果我想从另一个表中选择列我该怎么办?意思是当我点击组合框时,我想在文本框中显示来自两个不同表格的几列 这是我的代码:

private void comboLname_SelectedIndexChanged(object sender, EventArgs e)
{
    string conn = "Data Source=srv-db-02;Initial Catalog=rmsmasterdbtest;Persist Security Info=True;User ID=test;Password=*******";
    string Query = "select * from rmsmasterdbtest.dbo.customer where LastName= '" + comboLname.Text + "' ;";
    SqlConnection Myconn = new SqlConnection(conn);
    SqlCommand cmdDataBase = new SqlCommand(Query, Myconn);
    SqlDataReader Reader;
    try
    {
        Myconn.Open();
        Reader = cmdDataBase.ExecuteReader();
        while (Reader.Read())
        {
            string ID = Reader.GetInt32(Reader.GetOrdinal("ID")).ToString();
            string AccountNuber = Reader.GetString(Reader.GetOrdinal("AccountNumber"));
            //string Time = Reader.GetString(Reader.GetOrdinal("Time"));
            // string Deposit = Reader.GetString(Reader.GetOrdinal("Deposit"));
            string sstatus = Reader.GetString(Reader.GetOrdinal("status"));
            string slastname = Reader.GetString(Reader.GetOrdinal("lastname"));
            txtid.Text = ID;
            txtacnum.Text = AccountNuber;
            //txttime.Text = Time;
            //txtdeposit.Text = Deposit;
            txtstatus.Text = sstatus;
            txtlname.Text = slastname;
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    finally
    {
        Myconn.Close();
    }
} 

0 个答案:

没有答案