连接字符串错误

发布于 2024-08-08 12:50:08 字数 560 浏览 6 评论 0原文

Windows Server 2003 上的 IIS 出现问题(使用经典的 ASP Web 应用程序) 数据库服务器 (SQL Server 2005) 位于同一台计算机上。

我创建一个记录集,并按如下方式设置其活动连接:

Dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.ActiveConnection = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=mydatabase;User ID=myusername;Password=mypassword;"

问题是,当我设置此连接字符串时,会出现以下错误:

参数类型错误,超出可接受的范围,或者与一个冲突其他。

我知道这个连接字符串没有问题,因为当我使用我的开发服务器,但将连接指向远程数据库时,它工作得很好。

我尝试了许多连接字符串变体(来自connectionstrings.com),但他们给出了相同的错误。

Having a problem with IIS on Windows Server 2003 (using a classic ASP web application)
The database server (SQL Server 2005) is on the same machine.

I create a recordset, and set its activeconnection as follows:

Dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.ActiveConnection = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=mydatabase;User ID=myusername;Password=mypassword;"

The problem is that as SOON as I set this connection string, the following error is raised:

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

I know there isnt a problem with this connection string because when I use my development server, but point the connection toward the remote database, it works great.

I have tried many MANY connection string variations (from connectionstrings.com) and they have given the same error.

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

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

发布评论

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

评论(3

国粹 2024-08-15 12:50:08

ActiveConnection 属性采用 ADODB.Connection 类型的对象,而不是字符串。您不能直接分配连接字符串,您需要分配数据库连接。您的代码需要如下所示:

Set objDB = Server.CreateObject("ADODB.Connection");
objDB.Open = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=mydatabase;User ID=myusername;Password=mypassword;"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.ActiveConnection = objDB

实际上您根本不应该以这种方式使用 ActiveConnection 属性,您实际应该使用它来断开记录集以避免保持数据库连接打开不必要地:

Set objDB = Server.CreateObject("ADODB.Connection");
objDB.Open = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=mydatabase;User ID=myusername;Password=mypassword;"

//Get a Recordset and prep it for forward only disconnected use
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.CursorLocation = adUseClient
objRS.CursorType = adOpenStatic
objRS.LockType = adLockReadOnly
objRS.Open "SELECT * FROM SOME_TABLE", objDB

//Now disconnect the recordset and dispose of the database connection
Set objRS.ActiveConnection = Nothing
objDB.Close
Set objDB = Nothing

//Now do whatever you want with the Recordset
//...

The ActiveConnection property takes an Object of type ADODB.Connection and not a String. You can't assign a Connection String directly, you need to assign a database connection. Your code needs to look like the following:

Set objDB = Server.CreateObject("ADODB.Connection");
objDB.Open = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=mydatabase;User ID=myusername;Password=mypassword;"
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.ActiveConnection = objDB

Actually you shouldn't really use the ActiveConnection property in this way at all, what you should actually use it for is disconnecting a recordset to avoid keeping the database connection open unecessarily:

Set objDB = Server.CreateObject("ADODB.Connection");
objDB.Open = "Provider=SQLOLEDB;Network Library=DBMSSOCN;Data Source=xxx.xxx.xxx.xxx,1433;Initial Catalog=mydatabase;User ID=myusername;Password=mypassword;"

//Get a Recordset and prep it for forward only disconnected use
Set objRS = Server.CreateObject("ADODB.Recordset")
objRS.CursorLocation = adUseClient
objRS.CursorType = adOpenStatic
objRS.LockType = adLockReadOnly
objRS.Open "SELECT * FROM SOME_TABLE", objDB

//Now disconnect the recordset and dispose of the database connection
Set objRS.ActiveConnection = Nothing
objDB.Close
Set objDB = Nothing

//Now do whatever you want with the Recordset
//...
雨夜星沙 2024-08-15 12:50:08

@RobV - 创建连接对象并将其分配给记录集确实是一个选项,但是,快捷方式是将连接字符串分配给记录集 - 这确实有效。

事实上,以下代码是最有效的:

Dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open <sqlstatement>, <connectionstring>

我不确定我收到的错误发生了什么,但它突然停止了,没有更改任何代码!

@RobV - Creating a connection object and assigning that to the recordset is indeed an option, however, a shortcut to this is assigning a connection string to the recordset - which does work.

In fact the following code is the most efficient:

Dim RS
Set RS = Server.CreateObject("ADODB.Recordset")
RS.Open <sqlstatement>, <connectionstring>

Im not sure what happened with the error I was getting but it SUDDENLY just stopped without having changed any code!

浅暮の光 2024-08-15 12:50:08

尝试执行类似于 MSDN 示例 的操作。

它单独创建一个连接,然后分配它。

Try doing something similar to this MSDN Sample.

It's creating a connection separately, then assigning it.

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