在 Classic ASP 中创建动态 ADODB 连接

Creating dynamic ADODB connections in Classic ASP

我有一个数据库连接字符串列表,数据库名称。这些数据库具有相同的 table 结构。我想做的是动态地创建一个到每个连接的连接,add/delete/modify a table,但是,如果在任何地方弹出错误,则 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))

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

这可能会奏效:只需使用一组连接字符串。从这里你创建了一个连接数组。然后你可以迭代这个数组并将你的命令发送到单独的数据库。

  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

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'"