正文

Command对象2005-09-19 10:11:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/iamben250/4998.html

分享到:

Command对象                                                                      Command对象特定地为处理各种类型的命令而设计,特别是那些需要参数的命令。与Connection对象相似,Command对象可以运行返回记录集和不返回记录集两种类型的命令。实际上,如果命令不含有参数,那么它并不关心是使用Connection对象,还是Command对象,还是Recordset对象。                         9.2.1 返回记录集                                                       对于一个返回记录集的命令,可使用Execute方法。然而,与Connection对象不同,必须使用CommandText属性,而不能在Execute方法中使用命令文本。                               Set cmdAuthors =                         Server.CreateObject("ADODB.Command")                                cmdAuthors.CommandText = "Authors"                                Set rsAuthors = cmdAuthors.Execute                               这是告诉Command对象去执行一个简单的、返回一个只读记录集的命令的最简单方法。                               Execute方法也有一些可选参数,如表9-2所示:                        表9-2  Command对象的Execute方法的参数及说明                                                        参数RecordsAffected与Options同前面解释的一样,另外也可以使用CommandType属性设置命令类型:                               Set cmdAuthors =                         Server.CreateObject("ADODB.Command")                                cmdAuthors.CommandText = "Authors"                               cmdAuthors.CommandType = adCmdTable                               如果不设置其他参数,也可以在Execute行上设置,必须为它们使用逗号:                               Set rsAuthors = cmdAuthors.Execute(, , adCmdTable)                               在本章后面处理存储过程时,将会看到参数Parameters的用途。                               改变光标类型                                                       值得注意是,使用Execute方法返回的记录集具有缺省的光标类型。这意味着这是只能前移的、只读的记录集。虽然使用Execute方法不能改变这种情况,但对这个问题有一个解决的方法。                                                       如果需要使用一个命令,并且要求不同的光标和锁定类型,那么应该使用Recordset的Open方法,此时Command对象作为Recordset的数据源。例如:                               cmdAuthors.ActiveConnection = strConn                               cmdAuthors.CommandText = "Authors"                               cmdAuthors.CommandType = adCmdTable                                rsAuthors.Open cmdAuthors, , adOpenDynamic,                         adLockOptimistic                                                       注意,在Open命令行中忽略了连接细节,因为连接设置在Command对象中。连接细节在命令打开前已经设置在Command对象的ActiveConnection属性中。                         9.2.2 操作命令                               对于操作命令,比如那些无记录返回的更新命令,整个过程相似,只需移去设置记录集的代码:                               Set cmdUpdate =                         Server.CreateObject("ADODB.Command")                               strSQL = "UPDATE Titles SET Price = Price * 1.10"                         & "WHERE Type='Business'"                                cmdUpdate.ActiveConnection = strConn                               cmdUpdate.CommandText = sSQL                               cmdUpdate.CommandType = adCmdText                                cmdUpdate.Execute , , adExecuteNoRecords                                                       注意,我们在此设置了命令类型,然后在Execute行中增加了额外的设置选项。这段代码运行UPDATE命令,并且保证不会创建新的记录集。                        9.2.3 存储过程                                                       存储过程的使用是Command对象得到应用的一个领域。存储过程(有时也称存储查询)是存储在数据库中预先定义的SQL查询语句。                               为什么应该创建和使用存储过程而不是在代码中直接使用SQL字符串呢?主要有以下几个理由:                               ·                         存储过程被数据库编译过。这样可以产生一个“执行计划”,因此数据库确切地知道它将做什么,从而加快了过程的执行速度。                               ·                         存储过程通常被数据库高速缓存,这样使它们运行得更快,因为此时不需要从磁盘中读取它们。并非所有的数据库都支持这种缓存机制,比如微软的Access就不支持,而SQL                         Server却支持。                               ·                         通过指定数据库中的表只能被存储过程修改,可以确保数据更安全。这意味着具有潜在危险的SQL操作不会执行。                               · 可以避免将ASP代码和冗长的SQL语句混在一起,从而使ASP代码更易于维护。                               · 可以将所有SQL代码集中存放于服务器。                               · 可以在存储过程中使用输出参数,允许返回记录集或其他的值。                               一般说来,存储过程几乎总是比相当的SQL语句执行速度快。                                                       为了使用存储过程,只要将存储过程的名字作为命令文本,并设置相应的类型。例如,考虑前面更新书价的例子。如果在SQL                         Server上创建一个存储过程,可以编写代码:                               CREATE PROCEDURE usp_UpdatePrices                               AS                                      UPDATE Titles                                      SET        Price = Price * 1.10                                      WHERE  TYPE='Business'                               对于微软的Access数据库,可以使用一个简单的更新查询语句完成相同的任务,如图9-1所示:                                要在ASP网页中运行该存储过程,只需要使用以下代码:                               Set cmdUpdate =                         Server.CreateObject("ADODB.Command")                                cmdUpdate.ActiveConnection = strConn                               cmdUpdate.CommandText = "usp_UpdatePrices"                               cmdUpdate.CommandType = adCmdStoredProc                                cmdUpdate.Execute , , adExecuteNoRecords                               这只是运行存储过程。没有记录集返回,因为只是在更新数据。需要记住的是,除非确实需要,不要创建记录集。                                                       虽然这样做也可以,但并不是很灵活,因为仅仅处理一种类型的书。更好的做法是创建一个允许我们选择书类型的过程,这样就不必为每类书创建一个过程。同样也可去掉固定的10%更新,这样使得灵活性更好。那么,如何才能做到这一点呢,很简单,使用参数。                        1.  参数                        存储过程的参数(或变量)与一般的过程和函数的参数一样,可以传到函数内部,然后函数可以使用它的值。SQL                         Server(其他数据库也一样,包括Access)中的存储过程都具有这样的功能。                        为了使存储过程能处理多种类型的书,甚至允许用户指定价格的增加(或减少),需要增加一些参数:                        CREATE PROCEDURE usp_UpdatePrices                               @Type           Char(12),                               @Percent              Money                         AS                               UPDATE Titles                               SET        Price = Price * (1 + @Percent / 100)                               WHERE  Type = @Type                        现在,存储过程usp_UpdatePrices带有两个参数:                        · 一个是书的类型(@Type)。                        · 一个是书价变化的百分比(@Percent)。                        与VBScript的函数一样,这些参数都是变量。然而,与VBScript和其他脚本语言不同的是:在这些脚本语言中的变量都是variant类型,而SQL变量具有确定的类型(char、Money等等)。必须遵守SQL变量的命名规范,即变量必须以符号@开始。                        注意,我们让百分数作为一个整数(如10代表10%),而不是作为一个分数值传入此过程。这只是让存储过程变得更直观一些。                        2.  Parameters集合                        那么,现在有了带参数的存储过程,但如何通过ADO来调用它呢?我们已经见到了如何用Command对象调用不带参数的存储过程,实际上,它们之间并没有什么不同。不同之处在于Parameters集合的使用。                        Parameters集合包含存储过程中每个参数的Parameter对象。然而,ADO并不会自动地知道这些参数是什么,因此,必须用CreateParameter方法创建它们,采用下面的形式:                        Set Parameter = Command.CreateParameter (Name, [Type],                         [Direction], [Size], [Value])                        参数及说明如表9-3所示:                        表9-3  CreateParameter方法的参数及说明                         一旦创建了参数就可以将其追加到Parameters集合中,例如:                        Set parValue = cmdUpdate.CreateParameter("@Type",                         adVarWChar, adParamInput, _                                                                                                               12, "Business")                        cmdUpdate.Parameters.Append parValue                         Set parValue = cmdUpdate.CreateParameter("@Percent",                         adCurrency, _                                                                                                               adParamInput,  , 10)                        cmdUpdate.Parameters.Append parValue                        没有必要显式地创建一个对象去保存参数,缺省的Variant类型已经可以工作得相当好。如果不想创建一个变量,也可以走捷径,例如下面的代码:                        cmdUpdate.Parameters.Append =  _                        cmdUpdate.CreateParameter("@Percent", adCurrency,                         adParamInput,  , 10)                                                       这使用CreateParameter方法返回一个Parameter对象,并用Append方法接收它。这种方法比使用变量运行得快,却加长了代码行,可读性比较差。可以根据自己的爱好选择其中一种方法。                                                       参数加到Parameters集合后,就保留在其中,因此,不一定在创建参数时就为每个参数赋值。可以在命令运行前的任何时候设置参数的值。例如:                        cmdUpdate.Parameters.Append =  _                        cmdUpdate.CreateParameter("@Percent", adCurrency,                         adParamInput)                                cmdUpdate.Parameters("@Percent") = 10                        前一章提到了访问集合中的值有好几种方法,Parameters集合并没有什么不同。上面的例子使用参数的名字在集合中检索参数,也可以使用索引号进行检索:                        cmdUpdate.Parameters(0) = 10                        以上代码对参数集合中第一个(Parameters集合从0开始编号)参数进行了赋值。使用索引号比使用名字索引速度快,但很显然使用名字使代码更易读。                        重点注意Parameters集合中参数的顺序必须与存储过程中参数的顺序相一致。                        运行带参数的命令                        一旦加入参数,就可立即运行命令,同时这些参数的值传入存储过程。现在可用一个友好的网页去更新用户选择的类型的书价。例如,假设有一个名为UpdatePrices.asp的网页,其运行时的界面如图9-2所示:                                            图9-2  UpdatePrices.asp网页运行时的界面                        通过数据库中获取书类型的列表,可以很轻松地动态创建该页面。首先要做的是包含文件Connection.asp,该文件包含了连接字符串(保存在变量strConn中)以及对ADO常数的引用,这在前面的章节已经讨论过。                        <!-- #INCLUDE FILE="../Include/Connection.asp" -->                        接下来,可以创建一个窗体(在这儿不显示大量文本,仅仅用一个样本文件)。该窗体调用一个名为StoreProcedure.asp的文件。                        <FORM NAME="UpdatePrices" Method="post"                         ACTION="StoredProcedure.asp">                        <TABLE>                        <TR>                          <TD>Book Type:</TD>                          <TD><SELECT NAME="lstTypes"></TD>                        现在开始编写ASP脚本从title表中读取书的类型。使用一个SQL字符串只返回唯一的书类型,然后将返回值放到HTML的OPTION标记中:                        <%                          Dim recTypes                          Dim sDQ                           sDQ = Chr(34)          ' double quote character                           Set recTypes = Server.CreateObject("ADODB.Recordset")                           recTypes.Open "usp_BookTypes", strConn                           While Not recTypes.EOF                            Response.Write "<OPTION VALUE=" & sDQ &                         recTypes("type") & sDQ & _                                           ">" & recTypes("type")                            recTypes.MoveNext                          Wend                           recTypes.Close                          Set recTypes = Nothing                                                  %>                        显示书的类型后,接着可以构建窗体的其他部分,包括一个允许用户输入书价变化百分数的文本框。                        </SELECT>                          </TD>                        </TR>                        <TR>                          <TD>Percent Value</TD>                          <TD><INPUT NAME="txtPercent" TYPE="TEXT"></TD>                        </TR>                        </TABLE>                        <P>                        <INPUT TYPE="submit" VALUE="Run Query">                        </FORM>                        现在看一下Run                         Query按钮调用的ASP文件StoredProcedure.asp。首先,声明变量并从调用窗体取出书的类型和百分数。                        Dim cmdUpdate                        Dim lRecs                        Dim sType                        Dim cPercent                         ' Get the form values                        sType = Request.Form("lstTypes")                        cPercent = Request.Form("txtPercent")                        现在可以向用户显示一些确认信息,告诉他们将发生什么。                        ' Tell the user what's being done                        Response.Write "Updating all books"                        If sType <> "all" Then                          Response.Write " of type <B>" & sType & "</B>"                        End If                        Response.Write " by " & cPercent & "%<P>"                        现在重新回到代码内部,在此创建Command对象和参数。                        Set cmdUpdate = Server.CreateObject("ADODB.Command")                         With cmdUpdate                          .ActiveConnection = strConn                          .CommandText = "usp_UpdatePrices"                          .CommandType = adCmdStoredProc                        利用从前面网页的窗体中提取的数据值,使用快捷方法创建和增加参数。                        ' Add the parameters                        .Parameters.Append .CreateParameter ("@Type",                         adVarWChar, adParamInput, _                        12, sType)                        .Parameters.Append .CreateParameter ("@Percent",                         adCurrency, _                        adParamInput, , cPercent)                        现在,运行存储过程。                        ' Execute the command                          .Execute lRecs, , adExecuteNoRecords                        End With                        为了确认,可以告诉用户已经更新多少条记录。                          ' And finally tell the user what's happened                          Response.Write "Procedure complete. " & lRecs & " were                         updated."                           Set cmdUpdate = Nothing                        %>                        这样就有了两个简单界面。前者创建了一个供选择的项目列表,后者使用其中某个项目值更新数据。这是许多需要显示和更新数据的ASP页面的基础。                        3.  传递数组参数                        Parameters参数集合一般来说比较好用,但有时稍有麻烦(尤其对于新手)。好在有一种快捷方法,使用Execute方法的Parameters参数。例如,调用存储过程usp_UpdatePrices,但不使用Parameters集合。                        创建一个Command对象,并同前面一样设置其属性。                        ' Set cmdUpdate = Server.CreateObject("ADODB.Command")                         ' Set the properties of the command                        With cmdUpdate                               .ActiveConnection = strConn                               .commandText = "usp_UpdatePrices"                               .commandType = adCmdStroreProc                        但这里正是差异所在。我们仅是通过Execute方法传递参数给存储过程,而不是创建参数并添加到集合中。                               ' Execute the command                               .Execute lngRecs, Array(strType, curPercent),                         adExecuteNoRecords                        End With                        这里使用了Array函数,将单个变量转换为数组,以适于方法调用。这种方法当然也有缺点:                        · 只能使用输入参数。因为不能指定参数的类型和传递方向,而缺省为输入参数。                        · 如果要多次调用存储过程,这种方法速度就比较慢,因为ADO将向数据存储询问参数的内容及数据类型。                        集合方法和数组方法之间在速度上的差异非常之小,几乎可以忽略。所以,如果只有输入参数,可随便使用哪一种。实际上,人们更喜欢使用Parameters集合的方法,尽管它稍为繁琐,但是使参数的属性更加明确。                        4.  输出参数                        我们已经知道如何获得受命令影响的记录数,如果需要更多信息,却又不想返回一个记录集,怎么办?也许想从存储过程中返回两个或三个值,但又不想费心创建一个记录集。在这时,可以定义一个输出参数,其值由存储过程提供。                        例如,对于更新书价的程序,如果想在更新之后找出最高价格,可将存储过程改成:                        CREATE PROCEDURE usp_UpdatePricesMax                               @Type           Char(12),                               @Percent              Money,                               @Max            Money           OUTPUT                        AS                        BEGIN                               UPDATE Titles                               SET       Price = Price * (1 + @Percent / 100)                               WHERE  Type = @Type                                SELECT @Max = MAX(Price)                               FROM    Titles                        END                        这只是在执行更新后运行了一个简单的SELECT语句,并将值赋给输出参数。                        现在可以改写StroreProcedure.asp的代码从而获取变量@MAX的值。                        <%                          Dim cmdUpdate                          Dim lngRecs                          Dim strType                          Dim curPercent                          Dim curMax                           ' Get the form values                          strType = Request.Form("lstTypes")                            curPercent = Request.Form("txtPercent")                           ' Tell the user what's being done                          Response.Write "Updating all books" & " of type <B>" &                         strType & "</B>" & _                        " by " & curPercent & "%<P>"                           Set cmdUpdate = Server.CreateObject("ADODB.Command")                           ' Set the properties of the command                          With cmdUpdate                            .ActiveConnection = strConn                            .CommandText = "usp_UpdatePricesMax"                            .CommandType = adCmdStoredProc                        我们只是在集合中加入了另一个参数,但这次指定为输出参数。注意它并没有赋值,因为其值将由存储过程提供,记住这是一个输出参数。                            ' Add the parameters                            .Parameters.Append .CreateParameter("@Type",                         adVarWChar, adParamInput, _                        12, strType)                            .Parameters.Append .CreateParameter("@Percent",                         adCurrency, _                        adParamInput, , curPercent)                                      .Parameters.Append.CreateParameter("@Max",                         adCurrency, adParamOutput)                            ' Execute the command                            .Execute lngRecs, , adExecuteNoRecords                        一旦执行这个过程,就可从集合中取得该值。                               ' Extract the output parameter, which the stored                               ' procedure has supplied to the parameters                         collection                               curMax = .Parameters("@Max")                          End With                           ' And finally tell the user what's happened                          Response.Write "Procedure complete. " & lngRecs & _                        " records were updated.<P>"                                 Response.Write "The highest price book is now "                         & _                                                           FormatCurrency(curMax)                           Set cmdUpdate = Nothing                        %>                        如果有不止一个输出参数,可用相同的方法访问。可以使用参数名或索引号取出集合中的值。                         5.  返回值                        对函数返回值的处理不同于存储过程返回值的处理,这常常导致混淆。在函数中,经常是返回一个布尔值来表明函数运行的成功与否。                        If SomeFunctionName() = True Then                               ' Function succeeded                        但在调用一个存储过程时,却不能使用同样的方法,因为存储是用Execute方法运行的,同时返回一个记录集。                        Set rsAuthors = cmdAuthors.Execute                        如果得不到一个返回值,如何确定是否已正确执行存储过程?当发生错误时,会报告错误,这样就可使用前一章提供的错误处理代码来处理错误。但对于一些非致命的逻辑错误怎么办?                        例如,考虑向employee表添加一个新职员的情形。你可能不想防止两个职员同名的情况,但想注明这个情况。那么,可以使用一个返回值以表明是否已有同名的职员存在。存储过程如下:                        CREATE PROCEDURE usp_AddEmployee                               @Emp_ID             Char(9),                               @FName               Varchar(20),                               @Minit                  Char(1),                               @LName               Varchar(30),                               @Job_ID                     SmallInt,                               @Job_Lvl              TinyInt,                               @Pub_ID              Char(4),                               @Hire_Date           Datetime                        AS                        BEGIN                               DECLARE @Exists       Int                 --                         Return value                                -- See if an employee with the same name exists                               IF EXISTS(SELECT *                                              FROM          Employee                                              WHERE FName = @FName                                              AND            MInit = @MInit                                              AND            LName = @LName)                                  SELECT @Exists = 1                               ELSE                                  SELECT @Exists = 0                                INSERT INTO Employee (emp_id, fname, minit, lname,                                                           job_id, job_lvl,                         pub_id, hire_date)                               VALUES (@Emp_Id, @FName, @MInit, @LName, @Job_ID,                                              @Job_Lvl, @Pub_ID, @Hire_Date)                               RETURN @Exists                        END                        该过程首先检查是否有同名的职员存在,并据此设定相应的变量Exists,若存在同名,就设为1,否则为0。然后将该职员加到表中,同时把Exists的值作为返回值返回。                        注意尽管返回了一个值,但并未将其声明为存储过程的参数。                        调用该过程的ASP代码如下:                        <!-- #INCLUDE FILE="../include/Connection.asp" -->                        <%                          Dim cmdEmployee                          Dim lngRecs                          Dim lngAdded                           Set cmdEmployee = Server.CreateObject("ADODB.Command")                           ' Set the properties of the command                          With cmdEmployee                            .ActiveConnection = strConn                            .CommandText = "usp_AddEmployee"                            .CommandType = adCmdStoredProc                             ' Create the parameters                               ' Notice that the return value is the first                         parameter                            .Parameters.Append .CreateParameter ("RETURN_VALUE",                         adInteger, _                        adParamReturnValue)                            .Parameters.Append .CreateParameter ("@Emp_id",                         adChar, adParamInput, 9)                            .Parameters.Append .CreateParameter ("@fname",                         adVarWChar, adParamInput, 20)                            .Parameters.Append .CreateParameter ("@minit",                         adChar, adParamInput, 1)                            .Parameters.Append .CreateParameter ("@lname",                         adVarWChar, adParamInput, 30)                            .Parameters.Append .CreateParameter ("@job_id",                         adSmallInt, adParamInput)                            .Parameters.Append .CreateParameter ("@job_lvl",                         adUnsignedTinyInt, adParamInput)                            .Parameters.Append .CreateParameter ("@pub_id",                         adChar, adParamInput, 4)                            .Parameters.Append .CreateParameter ("@hire_date",                         adDBTimeStamp, _                        adParamInput, 8)                             ' Set the parameter values                            .Parameters("@Emp_id") = Request.Form("txtEmpID")                            .Parameters("@fname") = Request.Form("txtFirstName")                            .Parameters("@minit") = Request.Form("txtInitial")                            .Parameters("@lname") = Request.Form("txtLastName")                            .Parameters("@job_id") = Request.Form("lstJobs")                            .Parameters("@job_lvl") = Request.Form("txtJobLevel")                            .Parameters("@pub_id") = Request.Form("lstPublisher")                            .Parameters("@hire_date") =                         Request.Form("txtHireDate")                                ' Run the stored procedure                            .Execute lngRecs, , adExecuteNoRecords                                ' Extract the return value                            lngAdded = .Parameters("RETURN_VALUE")                          End With                           Response.Write "New employee added.<P>"                          If lngAdded = 1 Then                            Response.Write "An employee with the same name                         already exists."                          End If                           Set cmdEmployee = Nothing                        %>                        需要重点注意,返回值应当作为集合中第一个参数被创建。即使返回值并不作为一个参数出现在存储过程中,总是Parameters集合中的第一个Parameters。                        因此,特别强调一点:                        存储过程的返回值必须声明为Parameters集合中第一个参数,同时参数的Direction值必须为adParamReturnValue。                        使用返回值                        现在定义一个初始窗体,如图9-3所示:                         按下Add Employee按钮会产生如图9-4所示的显示:                         再添加同样的细节(ID号不同)会得到如图9-5所示的界面:                         6.  更新参数                        无需输入所有的参数细节,只需调用Refresh方法,就能让ADO完成更新。例如,假设已经创建了一个带有与前面例子相同的参数的过程usp_AddEmployee,并且没有改变运行的页面。                        With cmdEmployee                               .ActiveConnection = strConn                               .CommandText = "usp_Addemployee"                               .CommandType = adCmdStoredProc                        然后调用Refresh方法。                        .Parameters.Refresh                        这告诉ADO向数据存储请求每个参数的细节,并创建Parameters集合。然后可以为其赋值。                        .Parameters("@Emp_Id") = Request.Form("txtEmpID")                        .Parameters("@FName") = Request.Form("txtFirstName")                        .Parameters("@MInit") = Request.Form("txtInitial")                        .Parameters("@LName") = Request.Form("txtLastName")                        .Parameters("@Job_ID") = Request.Form("lstJobs")                        .Parameters("@Job_Lvl") = Request.Form("txtJobLevel")                        .Parameters("@Pub_ID") = Request.Form("lstPublisher")                        .Parameters("@Hire_Date") = Request.Form("txtHireDate")                        注意并不需要创建任何参数,包括返回值。                        这似乎真是一条捷径,但应意识到这种方法也造成了性能上的损失,因为ADO必须向提供者查询以获得存储过程的参数细节。尽管如此,这种方法还是很有用的,尤其是在从参数中取出正确的值有困难的时候。                        实际上,可以编写一个小实用程序作为开发工具使用,用来完成更新并建立Append语句,可以将其粘贴到自己的代码中。它看上去应该与图9-6所示的GenerateParameters.asp                         ASP页面类似。                         其代码相当简单。首先是包含连接符串和另一个ADOX常数文件。                        <!-- #INCLUDE FILE="../Include/Connection.asp" -->                        <!-- #INCLUDE FILE="../Include/ADOX.asp" -->                        接下来创建一个窗体,指定目标为PrintParameters.asp ASP页面。                        <FORM NAME="Procedures" METHOD="post"                         ACTION="PrintParameters.asp">                        Connection String:<BR>                        <TEXTAREA NAME="txtConnection" COLS="80" ROWS="5">                        <% = strConn %>                        </TEXTAREA>                        <P>                        Stored Procedure:<BR>                        <SELECT NAME="lstProcedures">                        然后,使用ADOX从SQL Server中得到存储过程的列表,同时创建一个含有这些存储过程名字的列表框。                        <%                          Dim catPubs                          Dim procProcedure                           ' Predefine the quote character                          strQuote = Chr(34)                          Set catPubs = Server.CreateObject("ADOX.Catalog")                           catPubs.ActiveConnection = strConn                           For Each procProcedure In catPubs.Procedures                           Response.Write "<OPTION VALUE=" & _                        strQuote & procProcedure.Name & _                        strQuote & ">" & procProcedure.Name                          Next                           Set procProcedure = Nothing                          Set catPubs = Nothing                        %>                        </SELECT>                        <P>                        <INPUT TYPE="submit" VALUE="Print Paramaters">                        </FORM>                        这是一个简单的窗体,包括一个用于显示连接字符串的TEXTAREA控件和用于显示存储过程名称的SELECT控件。以前没有见过的是ADOX,ADOX是数据定义与安全的ADO扩展,可以用来访问数据存储的目录(或是元数据)。                        本书不打算介绍ADOX的内容,但其十分简单。进一步的细节可参见《ADO Programmer's                         Reference》,Wrox出版社出版,2.1版或2.5版都行。                        上面的例子使用了Procedures集合,这个集合包含数据存储中的所有存储过程的列表。按下PrintParameters按钮时,将得到图9-7所示的显示:                         可以简单地从这里拷贝参数行到代码中。在前面使用了一个以前从未见过的包含文件。该文件包含了几个将ADO常数(例如数据类型、参数方向等)转换为字符串值的函数:                        <!-- #INCLUDE FILE="../Include/Descriptions.asp" -->                        接下来,定义一些变量,提取用户请求并创建Command对象。                        <%                          Dim cmdProc                          Dim parP                          Dim strConnection                          Dim strProcedure                          Dim strQuote                           ' Get the connection and procedure name from the user                          strQuote = Chr(34)                          strConnection = Request.Form("txtConnection")                          strProcedure = Request.Form("lstProcedures")                           'Update the user                          Response.Write "Connecting to <B>" & strConnection &                         "</B><BR>"                          Response.Write "Documenting parameters for <B>" & _                        strProcedure & "</B><P><P>"                           Set cmdProc = Server.CreateObject("ADODB.Command")                           ' Set the properties of the command, using the name                          ' of the procedure that the user selected                          With cmdProc                            .ActiveConnection = strConnection                            .CommandType = adCmdStoredProc                            .CommandText = strProcedure                        然后使用Refresh方法自动填写Parameters集合。                        .Parameters.Refresh                        现在可以遍历整个集合,写出包含创建参数所需的细节内容的字符串。                        For Each parP In .Parameters                            Response.Write ".Parameters.Append & _                        "("strQuote & parP.Name & _                        strQuote & ", " & _                        DataTypeDesc(parP.Type) & ", " & _                        ParamDirectionDesc(parP.Direction) & _                        ", " & _                        parP.Size & ")<BR>"                        Next                        End With                         Set cmdProc = Nothing                        %>                        在Descriptions.asp包含文件中可以找到函数DataTypeDesc和ParamDirectionDesc。                                                       Descriptions.asp包含文件以及其他的例子文件可以在Web站点http://www.wrox.com中找到。                        这是一个非常简单的技术,它较好地使用了Refresh方法。

阅读(3307) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册