设置datagridview的数据源为(DATASET)SQL查寻结果

 

private void button5_Click(object sender, EventArgs e)
02 {
03 if (MessageBox.Show("确认删除该行吗?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes )
04 {
05 SqlConnection conn = new SqlConnection();
06 conn.ConnectionString = "Data Source=PC2012081514KSL\\MSSQL2008;Initial Catalog=db_book;Integrated Security=True";
07 conn.Open();
08 int i = this.dataGridView1.SelectedRows.Count - 1;
09 Debug.WriteLine("AA={0}", i); //调试语句
10 for (int a = i; a >= 0; a--)
11 {
12 Debug.WriteLine("a={0}", a);//调试语句
13 string sqlstring = "delete from tb_book where book_id='"+dataGridView1 .SelectedRows [a].Cells [0].Value .ToString ()+"'";
14 SqlCommand cmd = new SqlCommand(sqlstring ,conn );
15 cmd.ExecuteNonQuery();
16 }
17 conn .Close ();
18 conn .Open ();
19 string sqlstring1="select * from tb_book";
20 SqlCommand cmd1=new SqlCommand (sqlstring1 ,conn );
21 SqlDataAdapter da=new SqlDataAdapter (cmd1);
22 cmd1 .ExecuteNonQuery ();
23 DataSet ds=new DataSet ();
24 da.Fill (ds );
25 dataGridView1 .DataSource =ds .Tables [0];
26 conn .Close ();
27 MessageBox .Show ("删除成功!");
28 }
29 else
30 {
31 return;
32 }
33 }

你可能感兴趣的:(设置datagridview的数据源为(DATASET)SQL查寻结果)