VBScript如何设置连接字符串

时间:2009-09-03 07:41:54

标签: sql-server-2005 vbscript connection-string database-connection wsh

我不确定之前是否已经讨论过但是我有以下代码(我从Access数据库的示例更改了。)

我不知道将什么内容放在 '提供商' '数据源' 中。我正在使用 MS SQL Server 2005 以及如何在我的机器中查找此信息?请即使我尝试从 http://www.connectionstrings.com/ 开始,但我仍然不知道该怎么做。


Dim conn, sql
sql = "SELECT * FROM tblOutbox"
Set conn = CreateObject("ADODB.Connection")
With conn
      .Provider = "myProvider"
      .Mode = adModeReadWrite
   .ConnectionString = "Data Source=mysource;" & _
          "database=myDbase.mdf;   "
      .Open 
End With
If conn.State = adStateOpen Then
      WScript.Echo "Connection was established."
End If
conn.Close
Set conn = Nothing

这里使用的是访问数据库:


Dim conn 
Dim sql
sql = "SELECT * FROM tblOutbox"
Set conn = CreateObject("ADODB.Connection")
   With conn
      .Provider = "Microsoft.Jet.OLEDB.4.0"
      .Mode = adModeReadWrite
      .ConnectionString = "Data Source= f:/Status.mdb"
      .Open 
   End With
   WScript.Echo "Connection was opened." 
conn.Close
Set conn = Nothing
WScript.Echo "Connection was closed."

4 个答案:

答案 0 :(得分:2)

满足您的所有连接字符串需求:http://www.connectionstrings.com/

答案 1 :(得分:1)

尝试在连接字符串中设置提供程序

conn.ConnectionString = "Provider=SQLOLEDB.1;Data Source=XXX;Initial Catalog=XXX;User ID=<XXX>;Password=<XXX>;"

注意:我没有测试过,但应该可以使用

答案 2 :(得分:1)

由于你的帮助,我终于找到了解决方案。 在管理工具,数据源(ODBS),驱动程序选项卡下,我使用提供程序:SQLNCLI,我的服务器:数据源,我将Recordset rs添加到代码中。似乎没有这个,就没有建立联系。我不知道发生了什么。

Dim conn , rs, sql, ConnString
sql = "SELECT * FROM tblOutbox"
Set rs = CreateObject("ADODB.Recordset")
Set conn = CreateObject("ADODB.Connection")
With conn
      .Provider = "SQLNCLI"
      .Mode = adModeReadWrite
      .ConnectionString = "SERVER=.\SQLExpress;AttachDbFilename=F:\Test2.mdf;Database=Test2.mdf; Trusted_Connection=Yes;"
      .Open
      WScript.Echo "Connection was established."
End With
rs.Open sql,conn
If conn.State = adStateOpen Then
      WScript.Echo "Connection was established."
Else
  WScript.Echo "No Connection ."
End If
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing

答案 3 :(得分:0)

使用SQL Server,您无法直接连接到该文件。 Access,作为基于文件的RDBMS有点不同。

按照你的例子,它看起来像这样:

Dim conn, sql
sql = "SELECT * FROM tblOutbox"
Set conn = CreateObject("ADODB.Connection")
With conn
      .Mode = adModeReadWrite
      .ConnectionString = "Provider=SQLOLEDB;server=[servername];database=[databasename]uid=[insertuser];pwd=[insertpassword];"
      .Open 
End With
If conn.State = adStateOpen Then
      WScript.Echo "Connection was established."
End If
conn.Close
Set conn = Nothing

通常会采用更紧凑的方式来做到这一点,但没有上下文很难给出更好的例子