ASP经典数据库连接

时间:2014-03-28 15:37:50

标签: html sql asp-classic

我想使用经典ASP打开和关闭与SQL Server数据库的连接,并让它从数据库运行一个过程。它没有参数。

2 个答案:

答案 0 :(得分:3)

这是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

答案 1 :(得分:2)

这是我一遍又一遍地使用的久经考验的方法。

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