简体   繁体   English

ASP经典数据库连接

[英]ASP Classic Database Connection

I want to use classic ASP to open and close a connection to a SQL Server database and let it run a procedure from the database. 我想使用经典的ASP打开和关闭与SQL Server数据库的连接,并使其从数据库运行过程。 It has no parameters. 它没有参数。

This is the connection details in ASP, change caps to the relevant information objDBRS(0) will be your first part of data from a select statement 这是ASP中的连接详细信息,更改有关信息的上限objDBRS(0)将是select语句中数据的第一部分

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

Here is a tried and tested approach I use over and over again. 这是我反复使用的一种久经考验的方法。

<%
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
%>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM