当前位置:编程学习 > asp >>

[MrYoung教程:易学之道]3ADO.NET基础之数据的增修删查

一、前文回顾
  上文中我们以一个数据的展示窗体像大家介绍了SQLDATAADAPTER,DATASET的基本使用,他是我们内存中的缓存数据集合,并介绍了如何绑定数据到我们的窗体控件和如何获取DATAGRIDVIEW当中的指定单元格的值。

 二、概述
  本文中我们将关注如何以拼接字符串的方式实现数据的增,修,删,查,并引出SQL注入的基本原理。

三、主要内容
  3.1 数据的增,修,删

  3.2 LIKE操作符及记录查询

  3.3 SQL注入原理

四、数据的增,修,删
  首先在我们的frmMain窗体中拖入如下控件并改名:

  4.1 新增记录:我们首先来实现数据的新增,但用户在上述的文本框中填入正确的姓名,密码,年龄等信息后,点击新增按钮,我们把数据存入数据库中。主要是获取各个文本框的值,然后拼接成正确的SQL语句,如insert into tb_user values(姓名,密码,年龄.......),然后调用SQL语句执行,具体代码如下:

view sourceprint?01       private void btn新增_Click(object sender, EventArgs e) 

02       { 

03           //sql字符串拼接成最后的插入语句 

04           string sql = "insert into tb_user (username,userpassword,userage,userphone,useraddress)"

05 +"values("+txtUserName.Text+","+txtPWD.Text+","+txtAge.Text+","+txtPhone.Text+","+txtAddress.Text+")"; 

06           //调用数据库操作方法执行语句 

07           BaseClass.BaseOperate.getcom(sql); 

08           //弹出对话框提示用户操作成功 

09           MessageBox.Show("新增用户成功!"); 

10           //重新执行查询并绑定数据窗体已显示新增的内容,相当于刷新数据窗体 

11           ds = BaseClass.BaseOperate.getds("select id as 编号,username as 姓名,userpassword as 密码,userage as 年龄,userphone as 电话,useraddress as 地址 from tb_user"); 

12           dataGridView1.DataSource = ds.Tables[0]; 

13           //情况各输入框的值 

14           reset(); 

15           //使光标停在第一个ID输入框中 

16           txtID.Focus(); 

17       } 

18       void reset() 

19       { 

20           txtAddress.Text = ""; 

21           txtAge.Text = ""; 

22           txtID.Text = ""; 

23           txtPhone.Text = ""; 

24           txtPWD.Text = ""; 

25           txtUserName.Text = ""; 

26       }

  

  BaseOperate中的getcom方法如下,用于执行插入,修改,删除操作:

 

view sourceprint?01 /// <summary> 

02 /// 执行SQLCOMMAND命令 

03 /// </summary> 

04 /// <param name="str_sql">要执行的SQL语句</param> 

05 public static void getcom(string str_sql) 

06 { 

07     using (SqlConnection sqlcon = new SqlConnection(connectionString)) 

08     { 

09         using (SqlCommand sqlcom = new SqlCommand(str_sql, sqlcon)) 

10         { 

11             try

12             { 

13  

14                 sqlcon.Open(); 

15                 sqlcom.ExecuteNonQuery(); 

16             } 

17             catch (Exception e1) 

18             { 

19                 sqlcon.Close(); 

20                 throw new Exception(e1.Message); 

21             } 

22  

23         } 

24     } 

25 }

  

  4.2 修改,删除方法相似,均为取得用户在dataGridView1中选取行的ID信息,通过ID(因为ID为数据库中每条记录的唯一标识)去操作数据库中的具体行数据。

 

view sourceprint?01 private void btn修改_Click(object sender, EventArgs e) 

02 { 

03     //获取用户选择行的用户名 

04     string strname = Convert.ToString(dataGridView1[1,dataGridView1.CurrentCell.RowIndex].Value); 

05     //弹出对话框提示用户是否要修改信息,如果用户点击确定则执行修改操作 

06     if (MessageBox.Show("你确定要修改" + strname + "用户么?","提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK) 

07     { 

08         string sql = "update tb_user set username=" + txtUserName.Text + ",userpassword=" + txtPWD.Text 

09             + ",userage=" + txtAge.Text + ",userphone=" + txtPhone.Text + ",useraddress=" + txtAddress.Text + " where id="

10             + Convert.ToString(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value); 

11         BaseClass.BaseOperate.getcom(sql); 

12  

13         MessageBox.Show("修改用户成功!"); 

14  

15         ds = BaseClass.BaseOperate.getds("select id as 编号,username as 姓名,userpassword as 密码,userage as 年龄,userphone as 电话,useraddress as 地址 from tb_user"); 

16         dataGridView1.DataSource = ds.Tables[0]; 

17         reset(); 

18         txtID.Focus(); 

19     } 

20 }

 

view sourceprint?01 private void btn删除_Click(object sender, EventArgs e) 

02 { 

补充:Web开发 , ASP.Net ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,