1 using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=StudentDB;User ID=sa;Password=123456")) 2 { 3 conn.Open(); 4 using (SqlCommand cmd = conn.CreateCommand()) 5 { 6 cmd.CommandText = "select * from T_student where Name=@Name"; 7 cmd.Parameters.Add(new SqlParameter("@Name", txtName.Text)); 8 //SqlDataAdapter是一个帮我们把SqlCommand查询结果填充到DataSet中的类 9 SqlDataAdapter adapter = new SqlDataAdapter(cmd); 10 //DataSet相当于本地的一个复杂集合(List<int>) 11 DataSet dataset = new DataSet(); 12 //把SqlCommand查询结果填充到DataSet中 13 adapter.Fill(dataset); 14 DataTable table = dataset.Tables[0]; 15 DataRowCollection rows = table.Rows; 16 for (int i = 0; i < rows.Count; i++) 17 { 18 DataRow row = rows[i]; 19 string name = (string)row["Name"]; 20 decimal score = (decimal)row["Score"]; 21 MessageBox.Show(name+","+score.ToString()); 22 } 23 24 } 25 }