<% 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数据库,读取数据并关闭连接。我研究了示例,并提出了这个建议。但它不起作用。请指导我。我要去哪里错了?
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/articles/database/datafunctions.asp?ID=AW