ASP SQL Server Connection

前端 未结 2 1930
自闭症患者
自闭症患者 2021-01-17 07:45
 <%
 DIM objConn
 Set objConn = Server.CreateObject(\"ADODB.Connection\")
 objConn.ConnectionString = \"Data Source=123.123         


        
相关标签:
2条回答
  • 2021-01-17 08:46

    Some answers have suggested wrapping logic into functions there is no need for this.

    It's just a lot of fluff that isn't needed, just use the ADODB.Command object. There are hundreds of ways to approach this but a method I have found to work time and again is let the ADODB.Command object do the work then return your results into an Array using .GetRows() method of the ADODB.Recordset object. That way you can close off both the ADODB.Recordset and ADODB.Command objects quickly and work just with the Array.

    Dim conn, cmd, rs, sql, data, search
    
    'Assume value to query comes from a Request Collection.
    search = Request("myvalue") & ""
    
    conn = "Data Source=123.123.12.123,1234;Database=DatabaseName;User Id=Usernm;Password=abcd1234;"
    sql = "select from mytable where this = ?"
    Set cmd = Server.CreateObject("ADODB.Command")
    With cmd
      'No need to handle connection let ADODB.Command create and destory it.
      .ActiveConnection = conn
      .CommandType = adCmdText
      .CommandText = sql
      .Parameters.Append(.CreateParameter("@myparam", adVarWChar, adParamInput, 50))
      .Parameters("@myparam").Value = search
      Set rs = .Execute()
      If Not rs.EOF Then data = rs.GetRows()
      Call rs.Close()
      Set rs = Nothing
    End with
    Set cmd = Nothing
    'ADODB.Connection is closed when ADODB.Command is destroyed.
    
    If IsArray(data) Then
      rows = UBound(data, 2)
      For row = 0 To rows
        'Return first column of the current row
        Call Response.Write("First Column of Row " & row & " is '" & data(0, row) & "'<br />"
      Next
    Else
      Call Response.Write("No records")
    End If
    
    0 讨论(0)
  • 2021-01-17 08:47
    Dim rs, dbConn
    
    Function OpenDB()
        Set dbConn = Server.CreateObject("ADODB.Connection")
        dbConn.ConnectionTimeout = 300
        dbConn.CommandTimeout = 300
        dbConn.Open "Data Source=123.123.12.123,1234;Database=DatabaseName;User Id=Usernm;Password=abcd1234;"
    End Function
    
    Function CloseDB()
        Set rs = Nothing
        if ucase(TypeName(dbConn)) = "CONNECTION" then
            dbConn.Close
            Set dbConn = Nothing
        end if
    End Function
    
    Function OpenRecordSet(recset, tablename)
        Call OpenDB()
        Set recset = Server.CreateObject("ADODB.Recordset")
        recset.Open tablename, dbConn, 0, 1
    End Function
    
    Function CloseRecordSet(recset)
        Set recset = Nothing
        Call CloseDB()
    End Function
    

    Then use

    <%
    Call OpenDB()
    sql = "select from mytable where this = 'that'"
    Set rs = dbConn.Execute(sql)
    if not rs.EOF then
          ' do your stuff!
    end if
    Call CloseDB()
    %>
    

    http://www.shiningstar.net/articles/articles/database/datafunctions.asp?ID=AW

    0 讨论(0)
提交回复
热议问题