技术文章 > 用数据库存取数据

用数据库存取数据

2018-07-20 07:06

文档管理软件,文档管理系统,知识管理系统,档案管理系统的技术资料:
作者: 飞刀
概述 这篇文章用了三种方法来存取步骤。这一切都是都是通过在Active Server Page中调用ActiveX Date Objects(ADO)来实现的! 更多的信息 下面的例子使用Command对象来实现。这个例子,存一个数字,最后也取出一个数字!
<%@ LANGUAGE="VBSCRIPT" %>


Place Document Title Here

This first method queries the data source about the parameters
of the stored procedure. This is the least efficient method of calling
a stored procedure.

<%
Set cn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
cn.Open "data source name", "userid", "password"
Set cmd.ActiveConnection = cn
cmd.CommandText = "sp_test"
cmd.CommandType = adCmdStoredProc
“ Ask the server about the parameters for the stored proc
cmd.Parameters.Refresh
“ Assign a value to the 2nd parameter.
“ Index of 0 represents first parameter.
cmd.Parameters(1) = 11
cmd.Execute
%>
Calling via method 1

ReturnValue = <% Response.Write cmd.Parameters(0) %>



Method 2 declares the stored procedure, and then explicitly declares
the parameters.

<%
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "data source name", "userid", "password"
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
cmd.CommandText = "sp_test"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Append cmd.CreateParameter("RetVal", adInteger, _
adParamReturnValue)
cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, _
adParamInput)
“ Set value of Param1 of the default collection to 22
cmd("Param1") = 22
cmd.Execute
%>
Calling via method 2

ReturnValue = <% Response.Write cmd(0) %>



Method 3 is probably the most formal way of calling a stored procedure.
It uses the canocial
<%
Set cn = Server.CreateObject("ADODB.Connection")
cn.Open "data source name", "userid", "password"
Set cmd = Server.CreateObject("ADODB.Command")
Set cmd.ActiveConnection = cn
“ Define the stored procedure“s inputs and outputs
“ Question marks act as placeholders for each parameter for the
“ stored procedure
cmd.CommandText = "{?=call sp_test(?)}"
“ specify parameter info 1 by 1 in the order of the question marks
“ specified when we defined the stored procedure
cmd.Parameters.Append cmd.CreateParameter("RetVal", adInteger, _
adParamReturnValue)
cmd.Parameters.Append cmd.CreateParameter("Param1", adInteger, _
adParamInput)
cmd.Parameters("Param1") = 33
cmd.Execute
%>
Calling via method 3

ReturnValue = <% Response.Write cmd("RetVal") %>




注意在上面的例子中,许多的Parameters属性被调用。有的是使用默认值,有些则是使用指定的值。