简体   繁体   中英

VBScript and SQL Server 2008 Questions

So I don't know much about SQL or VBScript, much less putting the two together.

I'm trying to connect to a SQL Server running SQL 2008. All I want to do is connect and I can't seem to do it. I keep getting various errors, including:

  • "Data source name not found and no default driver specified"
  • "Login failed for user 'user'"
  • "Invalid character"

I already know about http://www.connectionstrings.com but I don't know what a number of things mean. What is a Data Source? What is an Initial Catalog?

Here is what I have so far.

Dim result
Set cn = CreateObject("ADODB.Connection")
cn.ConnectionString = "Provider=SQLOLEDB.1;Server=MyIP;Database=SQLDatabase;User ID=user;Password=pass"
cn.Open
strQry = "select * from Users"
set result = cn.execute(strQry)

Can someone help me make heads and tails of this?

Edit 1: This is what I am seeing in the Server Configuration Manager;

在此处输入图片说明

Edit 2: I apologize for not making this clear. I am not connecting to the local machine. I am connecting to a machine on my network.

Data Source = the instance of the database engine you are connecting to, either a host name or a host name combined with a named instance. Often this is localhost or localhost\\SQLEXPRESS but it can also be an IP address or fully-qualified domain name (either inside or outside your network).

Initial Catalog = database to connect to. The login associated with the username/password used in the connection string must be in specific server roles or be associated with a user in the database that at least belongs to the public role.

If you've installed SQL Server Express Edition, your connection string should be:

cn.ConnectionString = "Provider=SQLNCLI10;" & _
  "Data Source=myIP\SQLExpress;" & _
  "Initial Catalog=SQLDatabase;" & _
  "User ID=user;Password=pass"

We can't know for sure if that is the correct connection string - if you've installed a default instance or you named your instance something else, for example. Other variations you might try (since you said you didn't install Express), in case there is some problem with myIP :

"Data Source=localhost;" & _

Or

"Data Source=(local);" & _

Or

"Data Source=.;" & _

Or

"Data Source=127.0.0.1;" & _

To see what you have actually installed, you can go to Start > All Programs > SQL Server 2008 > Configuration Tools > SQL Server Configuration Manager and click on SQL Server Services. In the right pane, you should see something like this:

在此处输入图片说明

Though your instance names may not match mine. If you see SQL Server (MSSQLSERVER) , this is a default instance, and the data source in your connection string would be . or (local) or (localhost) . If you see SQL Server (something other than MSSQLSERVER) then it would be .\\something other than MSSQLSERVER etc. You can also see whether the instance is running, which might be another problem. If the instance you want to connect to is stopped, you won't be able to connect to it until you right-click and choose Start. You might also want to right-click the instance and select Properties so that you can set the service to start automatically, so you don't have to repeat this process the next time you reboot. If you want the service to be running all the time.

If you show us what you see in the configuration manager we can help with the exact connection string you need.

EDIT

Now that I see the updated error message, which contains:

[DBNETLIB][ConnectionOpen (Connect()).]

You might try a few options:

(1) make sure TCP/IP is enabled. In the configuration manager, under SQL Server Network Configuration > Protocols for MSSQLSERVER, right-click TCP/IP, and choose Enable. You'll need to restart SQL Server. (While you should be able to use shared memory, this is how I used to always configure classic ASP.)

(2) add the following to your connection string:

cn.ConnectionString = "Provider=SQLNCLI10;" & _
  "Data Source=localhost;" & _
  "Initial Catalog=SQLDatabase;" & _
  "User ID=user;Password=pass;" & _
  "Network=DBMSSOCN;"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM