C#一个文本框输入查询后,多个文本框显示多条来自数据库中的相应内容

c#中由一个文本框输入查询条件,点击按钮后其他文本框中显示查询数据库中的内容(如图:)

以下是部分代码:
private void button1_Click(object sender, EventArgs e) { string conn = "Data Source=.\\SQLEXPRESS;Initial Catalog=E_Readingroom;Integrated Security=SSPI";//连接数据库信息 SqlConnection connection = new SqlConnection(conn); //创建连接 connection.Open(); //打开连接 string sql = string.Format("select name from SysA where ID='{0}'",textBox1.Text ); SqlCommand comm = new SqlCommand(sql, connection); //command对象 SqlDataReader dr = comm.ExecuteReader(); //定义数据读取对象 if (dr.Read()) { this.textBox2.Text = dr[0].ToString(); } connection.Close(); }
还往大家不吝赐教

 private void button1_Click(object sender, EventArgs e)
        {
            string conn = "Data Source=.\\SQLEXPRESS;Initial Catalog=E_Readingroom;Integrated Security=SSPI";//连接数据库信息
            SqlConnection connection = new SqlConnection(conn);                     //创建连接
            connection.Open();                                                      //打开连接
            string sql = string.Format("select name,dept from SysA where ID='{0}'",textBox1.Text );
            SqlCommand comm = new SqlCommand(sql, connection);                     //command对象          
            SqlDataReader dr = comm.ExecuteReader();                               //定义数据读取对象
            if (dr.Read())
            {
                this.textBox2.Text = (string)dr.GetValue(0);
                this.textBox3.Text = (string)dr.GetValue(1);
            }
            connection.Close();
        }

PS:没测试过,SQL语句改了 下,textBox.Text赋值改了下。测下看看是不是你要的效果

温馨提示:内容为网友见解,仅供参考
无其他回答
相似回答