【发布时间】:2014-03-31 22:45:05
【问题描述】:
我想使用经典 ASP 打开和关闭与 SQL Server 数据库的连接,并让它从数据库运行一个过程。它没有参数。
【问题讨论】:
标签: html sql asp-classic
我想使用经典 ASP 打开和关闭与 SQL Server 数据库的连接,并让它从数据库运行一个过程。它没有参数。
【问题讨论】:
标签: html sql asp-classic
这是 ASP 中的连接详细信息,将大写更改为相关信息 objDBRS(0) 将是您选择语句中数据的第一部分
Set objDBConn = Server.CreateObject("ADODB.Connection")
objDBConn.Open "Provider=sqloledb;Data Source=SQLSERVERNAME;Initial Catalog=DATABASENAME; User ID=Chris;Password=PASSWORD;"
Set objDBCommand = Server.CreateObject("ADODB.Command")
objDBCommand.ActiveConnection = objDBConn
objDBCommand.CommandText = "SQLPROCEDURENAME"
objDBCommand.CommandType = adCmdStoredProc
Set objDBRS = Server.CreateObject("ADODB.RecordSet")
objDBRS.open objDBCommand,,adOpenForwardOnly
DO WHAT YOU WANT HERE
Set objDBCommand=nothing
objDBConn.Close
Set objDBConn=nothing
【讨论】:
ADODB.Connection,将连接字符串传递给.ActiveConnection,它将在ADODB.Command 的整个生命周期内为您实例化它。 ADODB.Recordset 也很昂贵(除非您需要一些更不常用的功能)而不是使用 .GetRows() 将您的记录集转换为二维数组。允许ADODB.Command 实例化ADODB.Connection 的另一个好处是它不会在页面处理期间使用有价值的资源保持打开状态。
这是我反复使用的一种久经考验的方法。
<%
Dim cmd, conn_string, rs, data, row, rows
'Connection String if using latest version of SQL use SQL Server Native Client
'for more examples see http://www.connectionstrings.com/sql-server/
conn_string = "Provider=SQLNCLI11;Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"
Set cmd = Server.CreateObject("ADODB.Command")
With cmd
'No need to build ADODB.Connection the command object does it for you.
.ActiveConnection = conn_string
.CommandType = adCmdStoredProc
.CommandText = "[schema].[procedurename]"
Set rs = .Execute()
'Populate Array with rs and close and release ADODB.Recordset from memory.
If Not rs.EOF Then data = rs.GetRows()
Call rs.Close()
Set rs = Nothing
End With
'Release memory closes and releases ADODB.Connection as well.
Set cmd = Nothing
'Use Array to enumerate data without overhead of ADODB.Recordset.
If IsArray(data) Then
rows = UBound(data, 2)
For row = 0 To rows
'Read data
Call Response.Write("First Field: " & data(0, row))
Next
Else
'No records
Call Response.Write("No records to display")
End If
%>
【讨论】: