在Classic ASP中创建动态ADODB连接

时间:2015-01-27 16:12:37

标签: asp-classic adodb

我有一个数据库连接字符串列表,数据库名称。这些数据库具有相同的表结构。我想要做的是动态创建每个连接,添加/删除/修改表,但是,如果在任何地方弹出错误,那么RollbackTrans,否则,CommitTrans。

让我走上正确道路的基本问题是:

经典ASP中的此代码是否可以进行动态命名连接?

'create the dynamic object
execute("Set Con" & index & " = Server.CreateObject(""ADODB.connection"")")
'connect to the dynamic object
execute("Con" & index & ".Open " & DBString(index))

我得到的错误是'Expected end of statement'行(最后一行).open

2 个答案:

答案 0 :(得分:1)

这可能会起到作用:只使用一组连接字符串。从中您可以创建一个连接数组。然后,您可以迭代此数组并将命令发送到单独的数据库。

  dim connectionStrings(1)
  dim connections(1)
  dim curConn

  connectionStrings(0) = "Provider=sqloledb;Server=.\EXPRESS2012;Database=master;uid=youruser;pwd=yourpwd"
  connectionStrings(1) = "Provider=sqloledb;Server=.\EXPRESS2012;Database=model;uid=youruser;pwd=yourpwd"

  for curConn = 0 to ubound( connectionStrings)
    set connections(curConn) = Server.CreateObject("ADODB.Connection")
    connections(curConn).Open connectionStrings(curConn)
  next

  dim cmd : cmd = "select @@servername, db_name()"
  for curConn = 0 to ubound( connectionStrings)
    dim rs
    set rs = connections(curConn).Execute( cmd)
    Response.write( rs( 0) & ":" & rs(1) & "<br />")
    rs.close
    set rs = nothing
  next


  for curConn = 0 to ubound( connectionStrings)
    call connections(curConn).Close
    set connections(curConn) = nothing
  next

答案 1 :(得分:0)

Mysql动态连接字符串,t = 1到4的示例,四个不同的数据库连接conns(t)


dim conns(4)

Set Conns(1)=Server.Createobject("ADODB.Connection")

Conns(1).Open "DRIVER={MySQL ODBC 3.51 Driver}; SERVER=localhost;port=3306;DATABASE=dbname;UID=root;PASSWORD=pass;OPTION=3"
Conns(1).Execute "SET NAMES 'latin5'"
Conns(1).Execute "SET CHARACTER SET latin5"
Conns(1).Execute "SET COLLATION_CONNECTION = 'latin5_turkish_ci'"