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

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

 private void button1_Click(object sender, EventArgs e)

        {

            string conn = "Data Source=.\\SQLEXPRESSInitial Catalog=E_ReadingroomIntegrated 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赋值改了下。测下看看是不是你要的效果

使用count方法啊!比如数据库中有table1这样一个表格,文本框名为textbox1,数据库对象为db的话,如果使用linq来写就是,textbox1.Text = db.textbox1.count


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/9997816.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-04
下一篇2023-05-04

发表评论

登录后才能评论

评论列表(0条)

    保存