本文介绍了如何通过在c#中输入数据库中的成员id来检索成员名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经采用了两种形式帐户&的MemberInfo。现在在我插入ID时,相应的会员名称应显示在文本框中。



我尝试过:



I've taken two forms account & memberinfo. now in account when i insert ID the respective member name should be displayed in textbox.

What I have tried:

private void tbMemberID_TextChanged(object sender, EventArgs e)
{
   try
    {
        DataAccess oDataAccess = new DataAccess();
        con.Open();
        oDataAccess.cmd.CommandText = "select MemberName from MemberInfo where MemberID='" + tbMemberID.Text + "'";
        oDataAccess.cmd.ExecuteNonQuery();
        oDataAccess.cmd.Connection = con;
        TextBox tb = new TextBox();
        tb.Text = ((string)cmd.ExecuteScalar());

        tbMemberName.Text = tb.Text;
        con.Close();
    }
    catch (Exception ex)
    {
    }

}

推荐答案

ExecuteReader()



然后将所有数据加载到表或数据集中,然后你就可以获得存储在数据库表中的任何东西。



现在你的代码变成




then load all data into table or dataset and then you can able get any thing which is store in you table of database.

Now your code is become

private void tbMemberID_TextChanged(object sender, EventArgs e)
{
   try
    {
        DataAccess oDataAccess = new DataAccess();
        con.Open();
        oDataAccess.cmd.CommandText = "select MemberName from MemberInfo where MemberID='" + tbMemberID.Text + "'";
        
        oDataAccess.cmd.Connection = con;
        reader= oDataAccess.cmd.ExecuteReader();
        DataTable dt=new DtaTable();
        dt.Load(reader);
        if(dt.Rows.Count>0)
        {
          string name=dt.Rows[0]["MemberName "].ToString();
          tbMemberName.Text =name;
        }
       
        con.Close();
    }
    catch (Exception ex)
    {
    }
 
}







希望它能帮到你




Hope it will help you


这篇关于如何通过在c#中输入数据库中的成员id来检索成员名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 22:39