DataSet, sqlDataAdapter 使用
DataSet, sqlDataAdapter 使用
1 private void btnSearch_Click(object sender, RoutedEventArgs e) 2 { 3 long resault;//这个只是给下一行TryParse用的,没有实际用途 4 if (tbNumber.Text.Length < 7 || tbNumber.Text.Length > 11 || !long.TryParse(tbNumber.Text, out resault)) 5 { 6 MessageBox.Show("输入号码有误,无法查询。"); 7 return; 8 } 9 string numsection = tbNumber.Text.Substring(0, 7); 10 string connStr = ConfigurationManager.ConnectionStrings["dbConnStr"].ConnectionString; 11 using (SqlConnection conn = new SqlConnection(connStr)) 12 { 13 conn.Open(); 14 using (SqlCommand cmd = conn.CreateCommand()) 15 { 16 //cmd.CommandText = "select * from T_Nums where NumSection=1300000"; 17 cmd.CommandText = "select * from T_Nums where NumSection=@numsection"; 18 cmd.Parameters.Add(new SqlParameter("@numsection", numsection)); 19 DataSet ds = new DataSet(); 20 SqlDataAdapter adapter = new SqlDataAdapter(cmd); 21 adapter.Fill(ds); 22 //DataRow row = ds.Tables[0].Rows[0]; 23 if (ds.Tables[0].Rows.Count == 0) 24 { 25 MessageBox.Show("没有查询到结果。"); 26 return; 27 } 28 //foreach (DataRow row in ds.Tables[0].Rows) 29 //{ 30 // string AddressStr = row["AddressStr"].ToString(); 31 // MessageBox.Show(AddressStr); 32 //} 33 DataRow row = ds.Tables[0].Rows[0]; 34 MessageBox.Show(tbNumber.Text + "\n号码归属地为:" + row["AddressStr"] + 35 "\n号码类型为:" + row["NumType"] + "\n区号为:" + row["AreaCode"]); 36 } 37 } 38 }
浙公网安备 33010602011771号