1. SQLServer执行存储过程语句:
CREATE PROCEDURE newsstorage @Count INT AS select TOP (@Count) * from bds257174220_db.dbo.News return GO
2. ASP页面调用:
<% ' 创建连接字符串 Const strConnectionString = "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=hi;User ID=sa;Password=123456;" ' 创建ADODB.Connection对象 Dim objConn Set objConn = Server.CreateObject("ADODB.Connection") ' 打开连接 objConn.Open strConnectionString ' 创建ADODB.Command对象 Dim objCmd Set objCmd = Server.CreateObject("ADODB.Command") ' 设置Command对象的属性 With objCmd .ActiveConnection = objConn ' 关联到连接对象 '.CommandType = adCmdStoredProc ' 指定命令类型为存储过程 .CommandType = 4 ' 指定命令类型为存储过程 .CommandText = "newsstorage" ' 设置存储过程名称 ' 添加参数(如果有) '.Parameters.Append .CreateParameter("@Count", adVarChar, adParamInput, 50, "参数值") .Parameters.Append .CreateParameter("@Count", 3, 1, 50, 10) .Prepared = true '要求将SQL命令先预编译 End With '读取存储过程中的参数值 Response.Write objCmd("@Count") Response.Write objCmd(0) Response.Write objCmd.Parameters(0) '第1种读取数据的方法: Dim rs Set rs = objCmd.Execute if not rs.eof then Do While Not rs.EOF response.Write(rs("AddDate")) rs.MoveNext Loop 'response.redirect "https://www.sohu.com/" else response.redirect "https://www.baidu.com/" end if '第2种读取数据的方法: Set rs= Server.CreateObject("ADODB.Recordset") '建立记录集对象 rs.Open objCmd, , 1, 3 '生成查询结果 Do While Not rs.EOF response.Write(rs("AddDate")) rs.MoveNext Loop ' 清理对象 objCmd.ActiveConnection = Nothing Set objCmd = Nothing objConn.Close Set objConn = Nothing %>
3. 附加带参数.CreateParameter对应表:
CreateParameter参数说明(ASP)
cmd.CreateParameter("参数名称",类型,方向,大小)
Cmd.CreateParameter Name,Type,Direction,Size,Value
数据类型(Type)的值及其意义如下:
名称值 整数值 功能
adDBTimeStamp 135 日期时间数据类型
adDecimal 14 十进制整数值
adDouble 5 双精度小数值
adError 10 系统错误信息
AdGUID 72 全域性唯一识别字(Globally unique identifier)
adDispath 9 COM/OLE自动对象(Automation Object)
adInteger 3 4字节有符号整数
adIUnknown 13 COM/OLE对象
adLongVarBinary 205 大型2字节值
adLongVarChar 201 大型字符串值
adLongVarWChar 203 大型未编码字符串
adNumeric 131 十进制整数值
adSingle 4 单精度浮点小数
adSmallInt 2 2字节有符号整数
adTinyInt 16 1字节有符号整数
adUnsignedBigInt 21 8字节无符号整数
adUnsignedInt 19 4字节无符号整数
adUnsignedSmallInt 18 2字节无符号整数
adUnsignedTinyInt 17 1字节无符号整数
adUserDefined 132 用户自定义数据类型
adVariant 12 OLE对象
adVarBinary 204 双字节字符变量值
adVarChar 200 字符变量值
advarchar 202 未编码字符串变量值
adWchar 130 未编码字符串
方向(Direction)值的意义如下:
名称值 整数值 功能
adParamInput 1 允许数据输入至该参数当中
adParamOutput 2 允许数据输出至该参数当中
adParamInputOutput 3 允许数据输入、输出至该参数当中
adparamReturnValue 4 允许从一子程序中返回数据至该参数当中