ASP SQL Server连接 [英] ASP SQL Server Connection

查看:50
本文介绍了ASP SQL Server连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 <%
 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/article/database/datafunctions.asp?ID = AW

这篇关于ASP SQL Server连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆