1 private void button1_Click(object sender, RoutedEventArgs e) 2 { 3 using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=master;User ID=sa;Password=123456;")) 4 { 5 conn.Open(); 6 using (SqlCommand cmd = conn.CreateCommand()) 7 { 8 cmd.CommandText = "select waterwork from Table_1 where othersnum=@num"; 9 cmd.Parameters.Add(new SqlParameter("@num", textBox1.Text)); 10 using (SqlDataReader reader = cmd.ExecuteReader()) 11 { 12 while (reader.Read()) 13 { 14 string work = reader.GetString(0); 15 MessageBox.Show(work); 16 } 17 } 18 } 19 } 20 } 21 22 private void btnDataSet1_Click(object sender, RoutedEventArgs e) 23 { 24 using(SqlConnection conn=new SqlConnection ("Data Source=.;Initial Catalog=master;User ID=sa;Password=123456;")) 25 { 26 using(SqlCommand cmd=conn.CreateCommand()) 27 { 28 cmd.CommandText = "select * from Table_1"; 29 SqlDataAdapter adapter = new SqlDataAdapter(cmd); 30 DataSet dataset = new DataSet(); 31 adapter.Fill(dataset); 32 //foreach(DataRow row in dataset.Tables[0].Rows) 33 //{ 34 // int waternum = (int)row["waterwork"]; 35 // MessageBox.Show(waternum.ToString()); 36 //} 37 DataTable table = dataset.Tables[0]; 38 DataRowCollection rows = table.Rows; 39 for (int i = 0; i < rows.Count;i++ ) 40 { 41 DataRow r = rows[i]; 42 string waternum = (string)r["waterwork"]; 43 MessageBox.Show(waternum); 44 } 45 } 46 } 47 }