ADODB.Connection#424:需要对象

时间:2016-06-06 06:57:25

标签: vbscript asp-classic

对不起有太多疑问要看。 我的代码:

$stmt = $db->query('SELECT * FROM mcommerce_cart');

我的网页上显示的返回错误是:

  

#424:需要对象

任何想法导致了什么?请忽略我没有检查正确的connectonstring的事实,关键是它是Function GetConnection(ByVal Server, ByVal Database, ByVal UserID, ByVal Password) Dim conn On Error Resume Next Set conn = Server.CreateObject("ADODB.Connection") If Err.Number <> 0 Then Set conn = Nothing Response.Write "#" & Err.Number & ": " & Err.Description Response.End Exit Function End If CONST connStringFormat = "Provider=SQLOLEDB.1;Password=@Password;Persist Security Info=True;User ID=@UserID;Initial Catalog=@Database;Data Source=@Server" Dim myConnectionString myConnectionString = Replace(Replace(Replace(Replace(connStringFormat, "@Server", Server), "@Database", Database), "@UserID", UserID), "@Password", Password) conn.Open myConnectionString On Error GoTo 0 Set GetConnection = conn End Function 声明中的炸弹!

2 个答案:

答案 0 :(得分:0)

在查看我的代码一千次并走开之后,我从函数中取出了我的CreateObject调用,它工作了(为什么,为什么,为什么),即我将我的函数转换为子函数并使用全局变量。

Sub SetupConnection(ByVal Server, ByVal Database, ByVal UserID, ByVal Password)
    CONST connStringFormat = "Provider=SQLOLEDB.1;Password=@Password;Persist Security Info=True;User ID=@UserID;Initial Catalog=@Database;Data Source=@Server"

    Dim myConnectionString
    myConnectionString = Replace(Replace(Replace(Replace(connStringFormat, "@Server", Server), "@Database", Database), "@UserID", UserID), "@Password", Password)

    conn.Open myConnectionString
End Sub

Dim conn
Set conn = Server.CreateObject("ADODB.Connection")
SetupConnection ...

答案 1 :(得分:0)

好的,所以这种方法没有任何问题,只是它被称为(因为我没有看到任何调用代码而假设的位置)

虽然错误处理目前是一件好事,但On Error Resume Next并不是非常有用,只能解决实例化ADODB.Connection对象时不会经常发生的问题。

Function GetConnection(ByVal Server, ByVal Database, ByVal UserID, ByVal Password)
    Dim conn: Set conn = Server.CreateObject("ADODB.Connection")
    Const connStringFormat = "Provider=SQLOLEDB.1;Password=@Password;Persist Security Info=True;User ID=@UserID;Initial Catalog=@Database;Data Source=@Server"
    Dim myConnectionString: myConnectionString = Replace(Replace(Replace(Replace(connStringFormat, "@Server", Server), "@Database", Database), "@UserID", UserID), "@Password", Password)

    'You are more likely to get errors here, when trying to open the connection.
    Call conn.Open(myConnectionString)
    Set GetConnection = conn
    Set conn = Nothing
End Function

要正确调用此方法,您仍需要使用Set语句,以便正确实例化新对象。

例如,这将起作用

Dim conn: Set conn = GetConnection("YourServer", "YourDatabase", "YourUserName", "YourPassword")

但是这会失败,因为ADODB.Connection对象假设我们需要默认的字符串表示形式ConnectionString

Dim conn: conn = GetConnection("YourServer", "YourDatabase", "YourUserName", "YourPassword")

证明这是一个快速测试

Dim conn: conn = GetConnection("YourServer", "YourDatabase", "YourUserName", "YourPassword")
Call Response.Write("conn is " & TypeName(conn) & "<br />")

Set conn = GetConnection("YourServer", "YourDatabase", "YourUserName", "YourPassword")
Call Response.Write("conn is " & TypeName(conn) & "<br />")

输出:

conn is String
conn is Connection