下面我们将详细讲解如何在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()
Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号