asp.net 中数据库操作初步

asp.net 中数据库操作初步

一、定义oledbcommand类型变量:mycommand
  
  要对数据库进行增加、删除、修改的操作我们还需要根据myconnectio的类型定义一个oledbcommand或者sqlcommand对象(请注意如果myconnection是oledbconnection类型,那么只能用oledbcommand;如果myconnection是sqlconnection类型,那么那么只能用sqlcommand。这里假设myconnection是oledbconnection类)。
  
  方法一
  你可以象拖放myconnection一样拖放一个oledbcommand,并命名为 mycommand。
  方法二
  在(关联文件).cs文件中protected system.data.oledb.oledbconnection myconnection;下面手动添加:
  protected system.data.oledb.oledbcommand mycommand;在
  private void initializecomponent()中
  this.myconnection =newsystem.data.oledb.oledbconnection();的下一行下面手动添加:
  this.mycommand = new system.data.oledb.oledbcommand();即可完成对mycommand的定义
  说明:mycommand的作用是用来执行sql命令
  
  二、利用定义的myconnectio和mycommand对数据库进行增加、删除、修改
  
  首先我们需要连接并打开一个数据库(关于数据库的连接和打开的操作请察看我们以前的文章)。
  打开数据库:
  myconnectio.open();然后我们需要给mycommand指定要执行的sql命令 :
  mycommand.commandtext = "delete from admin";接着我们需要给mycommand指定数据源(对那个数据库执行sql命令):
  mycommand.connection = myconnection;然后我们执行mycommand命令即可:
  mycommand. executenonquery();如果我们在执行还有
  "delete from admin";后需要接着执行
  “insert into admin (admin_code,admin_pwd) values(‘aa’,’bb’)”,则我们只要再次指定mycommand指定要执行的sql命令 :
  mycommand.commandtext =“insert into admin (admin_code,admin_pwd) values(‘aa’,’bb’)”,然后执行mycommand. executenonquery();即可。(由于数据库未关闭,所以我们不需要也不可以再次myconnectio.open();,同理由于没有改变mycommand的数据源所以我们也没有必要再次指定mycommand.connection = myconnection;)
  
  下面我们将详细讲解如何在page_load()中对数据库的增加、删除、修改,最后我们再来总结一下executenonquery(),executescalar(),executereader的用法
  
  1、 增加新的记录
  private void page_load(object sender, system.eventargs e)
  {
  myconnection.open();’打开数据库
  mycommand1.commandtext = "insert into admin values(‘aaddq‘,‘as‘,‘ss‘)";
  mycommand1.connection = myconnection;
  mycommand1.executenonquery();’由于增加了一条记录,所以返回1
  //或者mycommand1.executereader();先增加一条记录,然后返回一个system.data.oledb.oledbdatareader类型的对象,该对象为:eof
  //或者mycommand1. executescalar();先增加一条记录,返回未实列化的对象
  myconnection.close();
  }2、 删除现有数据
  private void page_load(object sender, system.eventargs e)
  {
  myconnection.open();’打开数据库
  mycommand1.commandtext = "delete * from admin";
  mycommand1.connection = myconnection;
  mycommand1.executenonquery();’由于删除了n条记录,所以返回n
  //或者mycommand1.executereader();先删除n条记录,然后返回一个system.data.oledb.oledbdatareader类型的对象,该对象为:eof
  //或者mycommand1. executescalar();先删除n条记录,返回未实列化的对象
  myconnection.close();
  }
  3、 修改现有数据
  private void page_load(object sender, system.eventargs e)
  {
  myconnection.open();’打开数据库
  mycommand1.commandtext = "update admin set admin_code=’212’,admin_pwd=’43’ where admin_code=’23’";
  mycommand1.connection = myconnection;
  mycommand1.executenonquery();’由于修改了1条记录,所以返回n
  //或者mycommand1.executereader();先修改了1条记录,然后返回一个system.data.oledb.oledbdatareader类型的对象,该对象为:eof
  //或者mycommand1. executescalar();先修改了1条记录,返回未实列化的对象
  myconnection.close();
  }
  
  三、关于mycommand的executenonquery(),executescalar(),executereader方法的区别:
  
  1、executenonquery():执行sql,返回一个整型变量,如果sql是对数据库的记录进行操作,那么返回操作影响的记录条数,如果是
  sql="create table lookupcodes (code_id smallint identity(1,1) primary key clustered, code_desc varchar(50) not null)"那么在表创建成功后该方法返回 –1。
  例如:
  private void page_load(object sender, system.eventargs e)
  {
  myconnection.open();’打开数据库
  mycommand1.commandtext = "create table lookupcodes (code_id smallint identity(1,1) primary key clustered, code_desc varchar(50) not null)"; mycommand1.connection = myconnection;
  mycommand1.executenonquery();’首先建立一个lookupcodes表,然后返回-1
  //或者mycommand1.executereader();首先建立一个lookupcodes表,然后返回一个system.data.oledb.oledbdatareader类型的对象,该对象为:eof
  //或者mycommand1. executescalar();首先建立一个lookupcodes表,返回未实列化的对象
  myconnection.close();
  }
  2、 executescalar():执行sql,(如果sql是查询select)返回查询结果的第一行第一列,如果(如果sql不是查询select)那么返回未实列化的对象,因为对象未实列化,所以返回结果不能tostring(),不能equals(null),也就是说返回结果没有任何作用
  
  3、 executereader方法执行sql,(如果sql是查询select)返回查询结果的集合,类型是system.data.oledb.oledbdatareader,你可以通过此结果,获取查询的数据。如果(如果sql不是查询select)那么返回一个没有任何数据的system.data.oledb.oledbdatareader类型的集合(eof)
  
  四、总结:
  
  asp.net中对于数据库的操作方法很多,要实现统一个目标不同的人可能会采取不同的方法,就好像在asp中有的人喜欢用rs.addnew,有的人喜欢用”insert into”,主要是看个人的习惯,当然在性能上不同的方法可能会存在较大的差别,这个只能靠我们在平常的学习中一点一滴的积累经验的。另外顺便说一下asp.net页提供类似如下方式的操作方法:
  
  oledbcommand2.parameters("au_id").value = textbox1.text
  oledbcommand2.parameters("au_lname").value = textbox2.text
  oledbcommand2.parameters("au_fname").value = textbox3.text
  oledbcommand2.parameters("phone").value = textbox4.text
  oledbcommand2.parameters("address").value = textbox5.text
  oledbcommand2.parameters("city").value = textbox6.text
  oledbcommand2.parameters("st").value = textbox7.text
  oledbcommand2.parameters("zip").value = textbox8.text
  oledbcommand2.parameters("contract").value = checkbox1.checked
  cmdresults = oledbcommand2.executenonquery()  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值