Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.1 KiB

  1. <%@ LANGUAGE = PerlScript%>
  2. <html>
  3. <head>
  4. <meta name="GENERATOR" content="Tobias Martinsson">
  5. <title>ADO Stored Procedures</title>
  6. </head>
  7. <body>
  8. <BODY BGCOLOR=#FFFFFF>
  9. <!--
  10. ActiveState PerlScript sample
  11. PerlScript: The coolest way to program custom web solutions.
  12. -->
  13. <!-- Masthead -->
  14. <TABLE CELLPADDING=3 BORDER=0 CELLSPACING=0>
  15. <TR VALIGN=TOP ><TD WIDTH=400>
  16. <A NAME="TOP"><IMG SRC="PSBWlogo.gif" WIDTH=400 HEIGHT=48 ALT="ActiveState PerlScript" BORDER=0></A><P>
  17. </TD></TR></TABLE>
  18. <HR>
  19. <H3>ActiveX Data Objects (ADO) Stored Procedures</H3>
  20. Calling stored procedures can be done with the Command object. Store the name of the procedure in a scalar variable, set the ActiveConnection-property to either a Connection object or a valid ConnectionString and then specify the stored procedure to run and specify that the Commandis a stored procedure. The stored procedure used is named "get_customer" and looks as follows:
  21. <PRE>
  22. CREATE PROCEDURE get_customer @cust_id nchar(5)
  23. AS
  24. SELECT * FROM Customers WHERE CustomerID=@cust_id
  25. </PRE>
  26. It is written for the Northwind database, and to work with the parameters, from ADO you call <B>Refresh()</B> to get the Parameters that the stored procedure call takes, then set a valid value, and execute the procedure. The example was written for the Northwind database on SQL Server 7.0 and 2000, but can easily be modified to work with other databases.
  27. <BR><BR>
  28. <B>Output from stored procedure:</B><BR>
  29. <%
  30. # Import the constants
  31. #
  32. use Win32::OLE::Const 'Microsoft ActiveX Data Objects 2.5';
  33. # Set the name of the stored procedure to run
  34. #
  35. $stored_procedure = "get_customer";
  36. # Create the Command object
  37. #
  38. $cmd = $Server->CreateObject('ADODB.Command');
  39. # Set the string used to connect to the database
  40. #
  41. $cmd->{ActiveConnection} = (<<EOF);
  42. Provider=SQLOLEDB;
  43. Persist Security Info=False;
  44. User ID=sa;
  45. Initial Catalog=Northwind
  46. EOF
  47. # The text of the command to execute
  48. #
  49. $cmd->{CommandText} = $stored_procedure;
  50. # The type of the command -- will not work without it
  51. #
  52. $cmd->{CommandType} = adCmdStoredProc; # Very important
  53. # Refresh the parameters collection so that you have all parameters available
  54. #
  55. $cmd->Parameters->Refresh();
  56. # Set the value of the parameter to use
  57. #
  58. $cmd->Parameters('@cust_id')->{Value}='ALFKI';
  59. # Execute the Command
  60. #
  61. $rst = $cmd->Execute();
  62. # Get the number of fields
  63. #
  64. $count = $rst->Fields->{Count};
  65. # Loop the fields
  66. #
  67. for(my $i=0; $i<$count; $i++) {
  68. $Response->Write($rst->Fields($i)->{Name});
  69. $Response->Write(": ");
  70. $Response->Write($rst->Fields($i)->{Value});
  71. $Response->Write("<BR>");
  72. }
  73. undef($cmd);
  74. %>
  75. <%
  76. $url = $Request->ServerVariables('PATH_INFO')->item;
  77. $_ = $Request->ServerVariables('PATH_TRANSLATED')->item;
  78. s/[\/\\](\w*\.asp\Z)//m;
  79. $params = 'filename='."$1".'&URL='."$url";
  80. $params =~ s#([^a-zA-Z0-9&_.:%/-\\]{1})#uc '%' . unpack('H2', $1)#eg;
  81. %>
  82. <BR><BR>
  83. <A HREF="index.htm"> Return </A>
  84. <A HREF="showsource.asp?<%=$params%>">
  85. <h4><i>view the source</i></h4></A>
  86. </BODY>
  87. </HTML>