ITPub博客

首页 > 应用开发 > IT综合 > rs.open与connection.execute(sql)的区别

rs.open与connection.execute(sql)的区别

IT综合 作者:zhengnx 时间:2015-05-12 11:00:24 0 删除 编辑

[转]rs.open与connection.execute(sql)的区别

(2013-04-08 16:18:53)
标签:

rs.open与connection.

it

分类: 编程相关

 如果sql是update,insert,delete则应该使用conn.execute(sql)也可以不要括号conn.execute sql
 如果sql是select语句则应该使用set rs=conn.execute(sql)括号一定要,不能省略:这是因为vbscript的特性,带返回值的调用必须加括号,不带返回值的调用可以不要括号。

 不管是rs.open sql,conn还是conn.execute(sql) [这里的SQL是delete,update,insert]执行以后都会返回一个关闭的记录集,也就是说如果sql是插入,更新,删除语句,那么set rs=conn.execute(sql)中的RS就没什么意义。

1.conn.execute
sql="select * from admin where username='xiaozhu'"
set rs=conn.execute(sql)
执行完以后自动关闭记录集
最后只需要关闭连接就可以了
conn.close
set conn=nothing


2.rs.open
set rs=server.createobject("adodb.recordset")
sql="select * from admin where username='xiaozhu'"
rs.open sql,conn,1,1
可以自己设定一些参数,即锁定和游标的移动方式
最后要关闭记录集和连接
rs.close
set rs=nothing
conn.close
set conn=nothing


3.command.execute
sql="select * from admin where username='xiaozhu'"
set rs=command.execute(sql)

1.
set rs=conn.execute("如果是select语句") 得到的rs.recordcount=-1
rs.open sql,conn("sql为select语句") 得到的rs.recordcount为正常的记录数

2.
rs.open是打开数据库 conn.execute 是执行SQL指令
set rs=conn.execute(insert,update,delete)返回一个关闭了的记录集
set rs=conn.execute(select)返回一个没有关闭的记录集

3.
CONN.EXECUTE(SQL,RowsAffected,C)
参数含义:
SQL的值可以是SQL语句、表名、存储过程名,也可以是数据提供者所能接受的任意字符串。为了提高性能,最好为C参数指定合适的值
可选参数RowsAffected将返回INSERT、UPDATE或DELETE查询执行以后所影响的数目。这些查询会返回一个关闭的Recordset对象。
一个SELECT查询将返回RowsAffected值为-1并且返回带有一行或多行内容的打开的Recordset。
4.

conn.execute sql适合于不需返回recordset对象时使用,比如下面的代码:
sql="delete from enews where id="&cstr(id)
conn.execute sql

如果需要返回recordset对象,则使用如下代码:
sql="select from enews where id="&cstr(id)
set rs = conn.execute(sql)

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/7868752/viewspace-1649966/ ,如需转载,请注明出处,否则将追究法律责任。

下一篇: JS execCommand方法
请登录后发表评论 登录
全部评论

注册时间:2011-08-11

  • 博文量
    1305
  • 访问量
    20583633