该ActiveConnection
属性采用 Object 类型ADODB.Connection
而不是 String。您不能直接分配连接字符串,您需要分配数据库连接。您的代码需要如下所示:
Set objDB = Server.CreateObject("ADODB.Connection");
objDB.Open = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=mydatabase;User ID=myusername;Password=mypassword;"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.ActiveConnection = objDB
实际上,您根本不应该以这种方式真正使用该ActiveConnection
属性,您应该实际使用它来断开记录集以避免不必要地保持数据库连接打开:
Set objDB = Server.CreateObject("ADODB.Connection");
objDB.Open = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=mydatabase;User ID=myusername;Password=mypassword;"
//Get a Recordset and prep it for forward only disconnected use
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.CursorLocation = adUseClient
objRS.CursorType = adOpenStatic
objRS.LockType = adLockReadOnly
objRS.Open "SELECT * FROM SOME_TABLE", objDB
//Now disconnect the recordset and dispose of the database connection
Set objRS.ActiveConnection = Nothing
objDB.Close
Set objDB = Nothing
//Now do whatever you want with the Recordset
//...