ASP SQL 服务器连接

 <%
 DIM objConn
 Set objConn = Server.CreateObject("ADODB.Connection")
 objConn.ConnectionString = "Data Source=123.123.12.123,1234;Database=DatabaseName;User Id=Usernm;Password=abcd1234;"
 objConn.Open

 DIM mySQL

 mySQL = "SELECT * FROM [Users] WHERE [User ID]='1'"

 DIM objRS
 Set objRS = Server.CreateObject("ADODB.Recordset")
 objRS.open(mySQL, objConn)

 Response.Write objRS("FullName")

 objRS.Close
 Set objRS = Nothing
 objConn.Close
 Set objConn = Nothing
 %>

我想连接到 SQL Server 数据库,读取数据并关闭连接.我研究了这些例子并想出了这个.但它不起作用.请指导我.我哪里错了?

I want to connect to a SQL Server Database, read the data and close the connection. I have studied the examples and came up with this. But its not working. Please guide me. Where am I going wrong?

推荐答案

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

然后使用

<%
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/文章/数据库/datafunctions.asp?ID=AW

相关文章