SQL Express connection string hell ASP.Net

2020-07-13 09:05发布

问题:

SQL Express 2005 is running locally. I have a project written by another person running on the same machine. All I want to do is connect to it, can't be that hard right?

This is the one I use in my old classic ASP code to hit another database running on the same instance:

Provider=SQLOLEDB;Data Source=MYLAPTOP\MSSMLBIZ;Persist Security Info=True;User ID=TestUser;Password=letmein;Initial Catalog=TestDB

But trying a version of that makes the .net code throw a wobbler as he's written it using SQLServer drivers so its not liking the Provider stuff.

Here is the orginal connection string from his code:

Server=(local);Initial Catalog=TheDatabase;User Id=TheUser;Password=ThePassword;

I've been to http://www.connectionstrings.com/sql-server-2005 and tried several of the options from there, these all get "SQL Server does not exist or access denied" (what a lovely mixed error message that is!):

  • Data Source=localhost;Integrated Security=True;Initial Catalog=TheDatabase
  • Data Source=localhost\SQLEXPRESS;Integrated Security=True;Initial Catalog=TheDatabase
  • Data Source=MyLaptop\SQLEXPRESS;Integrated Security=True;Initial Catalog=TheDatabase
  • Server=MyLaptop\SQLEXPRESS;Initial Catalog=TheDatabase;User Id=TheUser;Password=ThePassword;

I've created logins for MyLaptop/IUSR_MyLaptop, MyLaptop/ASPNET, MyLaptop/IWAM_MyLaptop in SQL Express and given them all read/write permissions to my DB and set their default DB to be TheDatabase.

What the heck am I doing wrong and how can I debug the problem some more?

UPDATE: Special Thanks to Chris for all his pointers, got there in the end, if you are having similar problem please read all the comments there are lots of links and tips on how to track them down.

回答1:

With that error message in your comment you should run through the items in http://blogs.msdn.com/sql_protocols/archive/2007/05/13/sql-network-interfaces-error-26-error-locating-server-instance-specified.aspx

I presume the instance is running and does allow connections over tcpip?



回答2:

Can you advise exactly what is in the config?

Are you using the block - in which case a valid connection string would be:

<add name="connection" providerName="System.Data.SqlClient" connectionString="Data Source=localhost\MSSMLBIZ;Initial Catalog=TheDatabase;Integrated Security=True" />

or

<add name="connection" providerName="System.Data.SqlClient" connectionString="Data Source=localhost\MSSMLBIZ;Initial Catalog=TheDatabase;Integrated Security=False;User Id=TheUser;Password=ThePassword;Application Name=AppName;" />

Or are you getting the connection string from app settings - in which case I guess your provider is set in code inside the app itself?



回答3:

Shouldn't your datasource read: Data Source=localhost\sqlexpress too?



回答4:

You don't mention granting rights for 'TheUser' to access the database on the server - if you're restored from another server you may had a sid mismatch.

Try running

sp_update_users_login 'report'

against the db in question.

If it returns the user account in the report try:

sp_update_users_login 'update_one', 'theuser', 'theuser'

to remap things.