VBScript如何设置连接字符串

发布于 2024-08-04 07:03:13 字数 1239 浏览 5 评论 0原文

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

我不知道要在 'Provider' 中放入什么内容“数据源”。我正在使用 MS SQL Server 2005,如何在我的计算机中查找此信息?拜托,即使我尝试从这里开始http://www.connectionstrings.com/< /em>,但我还是不知道该怎么做。


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

这里使用access数据库:


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

I am not sure whether it has been disscussed before or not but I have the following code (I changed from example for an Access database.)

I don't know what to put inside the 'Provider' and the 'Data Source'. I'm using MS SQL Server 2005 and how to find this information in my machine? Please, even I try to follow from here http://www.connectionstrings.com/, yet I still don't know how to do it.


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

here is using access database:


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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我一直都在从未离去 2024-08-11 07:03:13

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

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

注意:我还没有测试过它,但它应该可以工作

Try setting the provider in the connection string

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

Note: I haven't tested it but it should work

迷乱花海 2024-08-11 07:03:13

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

For all your connection string needs: http://www.connectionstrings.com/

找回味觉 2024-08-11 07:03:13

感谢您的帮助,我终于找到了解决方案。
在“管理工具”、“数据源 (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

I finally found the solution thanks to your help.
Under the Administrative Tools, Data Source (ODBS), driver tabs, I use provider: SQLNCLI, my Server: Data Source, and I add Recordset rs to the code. It seems that, without this one, the connection is not established. I don't know what happened.

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

¢好甜 2024-08-11 07:03:13

使用 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

通常有一种更紧凑的方法来做到这一点,但如果没有上下文,很难给出更好的例子

With SQL Server you don't connect directly to the file. Access, being a file-based RDBMS is a little different.

Following your example, it would look something like this:

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

There's usually a more compact way of doing this, but without the context its hard to give a better example

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文