我刚开始学:
问个简单的,建了一个数据库,有列A、B、C、D,在textbox1中输入ABCD中的一个,想通过单击按键,在textbox2中显示所对应的列的数据。
跟绕口令似的,请老师们教教我,多谢啦
具体的代码如何写???在线等

解决方案 »

  1.   

    string strSql="Select "+textbox1.Text.Trim()+" from tb";
    然后就可以了
      

  2.   

    那如何在textbox2中显示出来呢?
      

  3.   

    我是这么写的
    if(TextBox1.Text=="A")
    {
    SqlConnection myConn = new SqlConnection("server=hp;uid=sa;pwd=sa;database=test");
    string strSql="Select "+TextBox1.Text.Trim()+" from Table1";
    SqlDataAdapter myCmd = new SqlDataAdapter(strSql, myConn);
    }
      

  4.   

    T0:那如何在textbox2中显示出来呢?楼主想在TextBox中怎么显示啊.
    你得到的数据是一个表中的列的所有数据啊,可能不止一条记录,把所有的记录串成一串?建议使用ListBox Or DataGrid Or DataGridView 吧.. 
      

  5.   

    Re:如何在textbox2中显示出来呢?SqlDataReader sd=sqlCommand.ExecuteReader();
    while(sd.Reade())
    {
     textbox1.Text+=sd[0].toString();
    }
    sd.Close();
      

  6.   

    以上语句 代替你的
    SqlDataAdapter myCmd = new SqlDataAdapter(strSql, myConn);
      

  7.   

    sqlCommand 是个SqlCommand对象
      

  8.   

    抱错---c:\inetpub\wwwroot\WebApplication15\WebForm2.aspx.cs(65): 找不到类型或命名空间名称“sqlCommand”(是否缺少 using 指令或程序集引用?)
    我已经添加了using System.Data.SqlClient;
    using System.Data.OleDb;
      

  9.   

    应该在程序的类中定义如下:
    SqlCommand sqlCommand = new SqlCommand();
    然后设置连接和命令
    你可以自己查查MSDN
    要学会自学,
      

  10.   

    多谢,Reade的定义怎么写啊?
    我刚开始学,正在一点一点地看,敬礼~~
      

  11.   

    TO:Reade的定义怎么写啊?SqlDataReader sd=sqlCommand.ExecuteReader();
      

  12.   

    if (TextBox1.Text == "A")
                    {
                        SqlConnection myConn = new SqlConnection("server=hp;uid=sa;pwd=sa;database=test");
                        string strSql = "Select " + TextBox1.Text.Trim() + " from Table1";
                        SqlCommand cmd = new SqlCommand(strSql, myConn);
                        SqlDataReader sdr = cmd.EndExecuteReader();
                        string str = "";
                        //循环读出数据
                        while (sdr.Read())
                        {
                            sdr += sdr.GetString(0);//如果该列是字符型
                            
                        }
                        TextBox2.Text = str;
                    }
      

  13.   

    晕,写错了.
    cmd.EndExecuteReader();->cmd.ExecuteReader();