Connection string error

IIS issue on Windows Server 2003 (using classic ASP web application) Database server (SQL Server 2005) is on the same machine.

I create a Recordset and set it to activate like this:

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, when I set this connection string, the following error occurs:

The arguments are of the wrong type, out of range, or in conflict with each other.

I know the problem is with this connection string because when I use my development server but point to a remote database connection it works fine.

I tried many LOTS of connection string options (from connectionstrings.com) and they gave the same error.

+2


source to share


3 answers


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

In fact, the following code is most efficient:



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

      

I'm not sure what happened with the error I was getting, but it SUDDENLY just stopped without changing the code!

+1


source


The property ActiveConnection

takes an object of type ADODB.Connection

, not a string. You cannot directly assign the connection string, you need to assign the database connection. Your code should look like this:

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 property ActiveConnection

this way at all, what you should actually be using is to disable the recordset to avoid messy opening the database 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;"

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

      

+1


source


Try something like this MSDN example .

It creates the connection separately and then assigns it.

0


source







All Articles