C# winform窗体设计-通过条件查询数据
2020-12-13 16:52
                         标签:winform   style   blog   http   io   color   ar   os   使用         在winform 数据库设计中,有时候需要通过条件查询对应的数据,并且将数据显示在文本框(or 富文本框)中,下面,小编将讲述通过一个条件: 首先,我们需要对数据库建立连接,并且执行数据库命令,在此之前,我们先创建一个winform窗体,窗体设计大概如下所示: ---->   SKILLS: -----------当查询结果可能返回多行多列时,需要使用DataReader读取返回的数据 C# winform窗体设计-通过条件查询数据 标签:winform   style   blog   http   io   color   ar   os   使用    原文地址:http://www.cnblogs.com/boy1025/p/4087701.html 在创建窗体后,我们应该进行书写代码阶段:
   在创建窗体后,我们应该进行书写代码阶段: 1             string s = "server=SAM_PC;database=MInformationDB;Integrated Security=true";
 2             SqlConnection con = new SqlConnection(s);
 3             con.Open();
 4             string sql =string.Format( "select * from Student where Grade=‘{0}‘",textBox1.Text);
 5             SqlCommand command = new SqlCommand(sql, con);
 6 
 7             SqlDataReader reader = command.ExecuteReader();
 8             while (reader.Read())
 9             {
10                 string id = (String)reader["Id"];
11                 string name = (String)reader["Name"];
12                 int age = (int)reader["Age"];
13                 string gender = (string)reader["Gender"];
14                 string major = (string)reader["Major"];
15                 textBox2.Text += String.Format("{0}\t{1}\t{2}\t{3}\t{4}\r\n", id, name, age, gender, major);
16             }
