We are already familiar with using SQL Server Stored Procedures in asp to perform database operations. But do you know that in table-level database access, we can also create and use stored procedures "? Access + ASP is an excellent combination of lightweight Web applications: simple, fast, and compatible, but the performance is usually not high. And
We are already familiar with using asp to call SQL Server Stored Procedures to perform database operations. But do you know that in table-level database access, we can also create and use stored procedures "? Access + ASP is an excellent combination of lightweight Web applications: simple, fast, and compatible, but the performance is usually not high. And
We are already familiar with using asp to call SQL Server Stored Procedures to perform database operations. But do you know that in table-level database access, we can also create and use stored procedures "?
Access + ASP is an excellent combination of lightweight Web applications: simple, fast, and compatible, but the performance is usually not high. In addition, ADODB is used. connection and Recordset objects are also inconvenient to execute SQL statements, because the parameter values of SQL statements with parameters are often spliced into strings, as a result, there is such a problem as "single quotes. One advantage of using stored procedures is that it supports SQL statement parameter values.
In fact, the so-called "Stored PRocedure" in Access (2000 and later versions) is incomparable with the Stored PRocedure in SQL Server. It can only be regarded as "Stored Procedure Lite", does not support multiple SQL statements, does not support logical statements (huh, after all, not a T-SQL) and so on, I do not know whether it is pre-compiled. However, just as the so-called "class" implemented by VBScript is only encapsulation, it can greatly promote the "beautification" of the code structure and the reusability of the program. The "lightweight stored procedure" of Access ", for specifications, database operations with a low chance of errors should also be helpful, and the performance may be improved.
Next I will translate the step by step method to introduce how to create a stored procedure in Access and then use it in ASP programs.
(1) create a "Stored Procedure" in Access"
I don't know what the Access application level is. For me, it is just a tool for creating MDB database files. I will only create MDB files, create tables, indexes, and constraints on the Access interface ~
In Access, "query" assumes the role of a stored procedure. The "Stored Procedure" or "query" of Access mentioned below refer to this item.
For the creation of "query", Access provides a dumb tool, similar to the wizard used to create a DataAdapter in VS. NET. However, I like to write SQL code directly.
Well, let's take a look at the table structure of the database used in this simple example.
Click "query" on the left of the Access interface, and then double-click "create query in design view" on the right to open the query design view.
In this case, a visual query generator is displayed. We first add the tables to be involved in the SQL statement.
After adding a table, right-click the design view and select "SQL View" to switch to the SQL code editing window.
Okay. Let's talk about the features of the Access stored procedure.
Access query, I feel like it is a packaging of SQL statements, maybe some optimization, such as pre-compilation. We cannot use multiple operations, transactions, logical judgment, loops, etc. like writing SQL Server stored procedures ......
However, the main purpose of using the Access stored procedure is to use the extra query provided by parameters. Using the stored procedure, we do not have to face any troubles when splicing the parameter values into SQL statement strings, for example:
Code:
Dim SQL
SQL = "SELECT * FROM Users WHERE UserName = '" & userName &"'"
In the preceding Code, if the string userName contains single quotes ('), an error is returned. We must manually convert:
Code:
Dim SQL
SQL = "SELECT * FROM Users WHERE UserName = '" & Replace (userName, "'", "'' ") &" '"' is converted to two consecutive single quotes
When using a query with parameters, our SQL statement can be written as follows:
Code:
Dim SQL
SQL = "SELECT * FROM Users WHERE UserName = @ userName"
Then, pass the value of @ userName in the Parameter attribute of the Command object, which is convenient and intuitive.
Code:
With cmd
'Create a parameter object
. Parameters. Append. CreateParameter ("@ userName ")
'Specify values for each parameter
. Parameters ("@ userName") = userName
End
The usage of parameters in the Access stored procedure is also described here. Unlike the SQL Server Stored Procedure, the @ variable is used to specify parameters, and the input parameter objects with the same name are different. Parameters in Access are identified by "order" rather than "name. You do not need to specify the name of the input parameter. You can specify the name of the parameter in the SQL statement as long as the parameter value is entered. Generally, we use the Execute method of the Command object to directly input an array of parameter values for execution ~
Code:
Cmd. Execute, Array (userName)
Another example is to write an Access stored procedure as follows:
Code:
Select * from Users where UserName = p_UserName and BookTitle = p_bookTitle
You can do this by passing in an array of parameter values, but the order must correspond:
Code:
Cmd. Execute, Array (userName, bookTitle)
OK. Let's take a look at the two queries used in our example. One is to write data. Write the SQL statement, save it, and name it.
Another Stored Procedure Code for reading data.
(2) Use stored procedures
Then we can call these stored procedures in the ASP program.
Here we can see why the query in Access is its Stored procedure-the CommandType attribute of our Command object is set to 4, that is, Stored Proc!
So...
The following code is simple:
Code:
<%
Option Explicit
Dim s
Randomize
S = Rnd * 100
Dim conn, cmd
Set conn = Server. CreateObject ("ADODB. Connection ")
Set cmd = Server. CreateObject ("ADODB. Command ")
Conn. Open "Provider = Microsoft. Jet. OLEDB.4.0; Data Source =" & Server. MapPath ("sp. mdb ")
With cmd
. ActiveConnection = conn
. CommandType = & h0004' Stored Procedure
. CommandText = "AddNewData"
End
Cmd. Execute, Array (CStr (Now (), CSng (s ))
With cmd
. ActiveConnection = conn
. CommandType = & h0004' Stored Procedure
. CommandText = "GetData"
End
Dim resultRS, resultArray
Set resultRS = cmd. Execute (, Null)
If Not resultRS. EOF Then
ResultArray = resultRS. GetRows ()
End If
Set resultRS = Nothing
Set cmd = Nothing
Conn. Close
Set conn = Nothing
Response. Write"
"
Dim I
For I = 0 To UBound (resultArray, 2)
Response. Write"
- "& ResultArray (0, I)
Response. Write "" & resultArray (1, I)
Response. Write "" & resultArray (2, I)
Response. Write" "
Next
Response. Write"
"
%>
Running result.
It seems like the speed is very fast ~
I don't know how to use the stored procedure in Access is of little significance, but it is really fun.
ASP is declining, but I still like it ~
References:
Http://aspalliance.com/andrewmooney/default.aspx? Article = 16
Http://support.microsoft.com/default.aspx? Scid = kb; EN-US; 304352