Cannot open database 'dbname' requested by the login. The login failed - asp.net

Firstly, I know that there are a lot of similar topics, but none seem to solve my problem.
I have inherited a project where the front-end is built in ASP.NET and the CMS is built in classic ASP. I'm in the process of setting up a local development environment where I've copied the database from the live server and am configuring the website to connect to my new local DB.
The ASP.NET part is connecting with the connection string Data Source=.\\SQLEXPRESS; Database=DBNAME; Trusted_Connection=True; but I had to mess about with Application Pools and Permissions.
I'm having a problem with getting the classic ASP part of the website to connect to the SQL database. The error I am recieving is:
Microsoft OLE DB Provider for ODBC Drivers error '80004005'
[Microsoft][ODBC SQL Server Driver][SQL Server]Cannot open database "DBNAME" requested by the login. The login failed.
/cms/connections/Conn.asp, line 18
The code in that file is:
12. Dim Conn_STRING, URL, mailHost
13. Conn_STRING = "Driver={SQL Server}; Server=.\SQLEXPRESS; Database=DBNAME; Trusted_Connection=True;"
15. Dim conn
16. set conn = Server.CreateObject("ADODB.Connection")
17. conn.ConnectionString = Conn_STRING
18. conn.Open
I know it's a permission problem or some sort, but I can't get to the bottom of it. I'll appreciate any help at all :)

hmmmm, well, the asp.net part is using SQLExpress, connected via an attached file. DBNAME in the first example is going to refer to a db file name in the actual asp.net project. It's been quite a long time since I last used classic ASP, but I'd be STUNNED if it supports this. You need to attach the db file to an actual sql server and use it that way.
server=mycomputername\sqlexpress; database=DBNAME; user id=username; password=password

You'll need to open your database in sql express mngmt studio
http://www.microsoft.com/download/en/details.aspx?id=7593
Go into your security settings for that user and set the default database to "dbname" (whatver your real dbname is of course)
Ensure your app pool is set to run under the user that has permissions to that database. It may just be easier to enable mixed mode authentication and create a sql user to connect to the database rather than messing around with windows authentication on an app pool.
You wil lof course need to change your connection string then to support the sql authentication if you go that route.
http://msdn.microsoft.com/en-us/library/ms143705%28v=sql.90%29.aspx

Related

Azure login to user db with pyodbc from a Azure webjob

I am trying to login to Azure sql server with pyodbc which logs me in a master db rather than the userdb like 'xyzdb' that i provide in the connection string. The solution to this seems changing to a DSN less based logins that needs configuring the dsn on windows machine.
But i require to run the python script with pyodbc connection from a Azure webjob so configuring dsn is not possible. Hence not able to use the user database as intended. Any solution?
After login to master db i thought to change to user db with "USE xyzdb;" Sql command to which the azure sql server responded 'USE cannot be used to switch between databases' hence xyzdb login cannot be made. I am stuck :).
I made the azure sql server with xyzdb database. Then i tried connection string for newly created server:
pyodbc.connect('Driver={ ODBC Driver 13 for SQL Server};Server=tcp:abc.database.windows.net,1433;Database=xyzdb;
Uid=ur_username;Pwd=ur_password;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;')
This connection string logged me in masterdb not in xyzdb as intended. And As Sql server are physically maintained in stack in data center interconnection is not everytime feasible unless using something as pool elastic. So 'USE xyzdb;' Sql statement from master DB was providing 'USE cannot be used for switching between databases'.
for 3 days i tried with other methods but it didn't worked,so i posted the question.Then i tried the connection string as:
pyodbc.connect('Driver={ODBC Driver 13 for SQL Server};Server=tcp:abc.database.windows.net,1433;
Uid=ur_username;Pwd=ur_password;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Database=xyzdb')
moving the Database field at the end of the string thinking it will first make a connection with server then it will log me in database xyzdb and It logged me in the xyzdb database.It worked the first time itself.
Now if i try with any of the 2 strings above its working as expected. Don't know what was the issue?.Looks like i wasted time of others as well :). we can remove the question i presume, as i can login the userdb. Thanks.

Classic ASP connecting to localdb for SQL Server

Can anyone explain why this works
Conn.Open "Provider=SQLOLEDB;Server=tcp:myazurename.database.windows.net;Database=People;Uid=MyUid;Pwd=MyPwd;Encrypt=yes;"
and this doesn't
Conn.Open "Provider=SQLOLEDB;Server=(localdb)\v11.0;Database=People;Uid=MyUid;Pwd=MyPwd;Encrypt=yes;"
I copied my live azure database locally for dev work in Classic ASP and the error message is
Microsoft OLE DB Provider for SQL Server error '80004005'
[DBNETLIB][ConnectionOpen (Connect()).] SQL Server does not exist or access denied.
Azure SQL server version: 11.0.9231
Local SQL server version: 11.0.2318
I can connect using the same details on SQL Server Management Studio so its not a user permissions thing...
Under Security for the server, the user has public server role, the user mapping for the 'People' database is everything except db_denydatareader and db_denydatawriter.
I have also tried (localdb)\\v11.0 which doesn't make a difference.
Any ideas?
OK so I believe the answer is as follows: It doesn't like (LocalDb)\V11.0 it wants a proper instance name like you said in the first place. MACHINENAME\INSTANCENAME OR localhost\INSTANCENAME which I am using as this is going to be replicated for other devs. Also, you DONT NEED two back slashes! My problem is I somehow didn't have a local instance installed. Thanks or your help #Lankymart

User Instance in a connection string gives an error

When program is trying to open the database I get:
Error message: Cannot open database "Chinatowndb" requested by the login. The login failed.
Login failed for user 'Lee-VAIO\Lee'.
Code is for connection string:
conn = new SqlConnection(#"Data Source=.\SQLEXPRESS;Initial Catalog=Chinatowndb;Integrated Security=True;User Instance=True");
If I change the code to:
conn = new SqlConnection(#"Data Source=.\SQLEXPRESS;Initial Catalog=Chinatowndb;Integrated Security=True");
It works. Now I have another program that uses the first connection string, different database, and it works. Note that the another program has the .mdf file with the App_Data folder. Chinatowndb.mdf is store at C:\Program Files\Microsoft SQL Server\MSSQL10_50.SQLEXPRESS\MSSQL\DATA.
Why the error?
For mdf database you must change string connection format, it's not same
Server=.\SQLExpress;AttachDbFilename=c:\mydbfile.mdf;Database=dbname; Trusted_Connection=Yes;
Or
Server=.\SQLExpress;AttachDbFilename=|DataDirectory|mydbfile.mdf;
Database=dbname;Trusted_Connection=Yes;
Link :
first one will work if you do this
Go to Query Window in SQL Server Management Studio and type this:
exec sp_configure 'user instances enabled', 1.
Go
Reconfigure
Also Read below
Using an User Instance on a local SQL Server Express instance
The User Instance functionality creates a new SQL Server instance on the fly during connect. This works only on a local SQL Server instance and only when connecting using windows authentication over local named pipes. The purpose is to be able to create a full rights SQL Server instance to a user with limited administrative rights on the computer.
Data Source=.\SQLExpress;Integrated Security=true; AttachDbFilename=|DataDirectory|\mydb.mdf;User Instance=true;
To use the User Instance functionality you need to enable it on the SQL Server. This is done by executing the following command: sp_configure 'user instances enabled', '1'. To disable the functionality execute sp_configure 'user instances enabled', '0'.
In my case, I excluded the database.mdf file from project, deleted the AttachDatabase:database.mdf from connection string and set user instance=false.
Working fine for me. Able to get data from Database.

DSN tests ok, but login failure trying to use the DSN to SQL Server 2005

I'm writing a small ASP.NET application in a hosted environment (meaning I don't own the server).
Using the hosting provider's webtools, I created a DSN that specifies the Driver, the Server, the UID, the PWD, and the Database. When I test the connection, it tests out fine.
However, when I load my web page with the code:
OdbcConnection DB = new OdbcConnection("DSN=MyDSNName");
DB.Open();
I get the error: ERROR [28000] [Microsoft][ODBC SQL Server Driver][SQL Server]Login failed for user ''.
I know I'm using the correct DSN name because when I change to "DSN=NonExistentDSN" I get a different error.
I don't understand why the logon works when I test it but not when I use it in code. Since I don't own the server, some of the usual troubleshooting tools aren't available to me, but I'd appreciate any feedback the community has.
Perhaps the DSN does not retain the password. Have you tried to supply login credentials?
OdbcConnection DB = new OdbcConnection("DSN=MyDSNName;UID=login;PWD=password;");
DB.Open();

Why do I get an ADO connection error while using classic ASP?

I am getting an error when running this classic asp script:
Dim DB_CONNECTIONSTRING, rs, iRecordCount, strSQL
DB_CONNECTIONSTRING = "Provider=SQLOLEDB;Data Source=localhost;Initial Catalog=employee;Trusted_Connection=yes;"
strSQL = "SELECT * FROM EmployeeProfiles"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open strSQL, DB_CONNECTIONSTRING, adOpenStatic, adLockReadOnly, adCmdText
The message I am getting is (the server does exsist):
Microsoft OLE DB Provider for SQL
Server error '80004005'
[DBNETLIB][ConnectionOpen
(Connect()).]SQL Server does not exist
or access denied.
\Default.asp, line 13
I see you're using Trusted_Connection=yes in your connection string. That means that whatever identity ASP is running under will try to connect to the database server using Windows authentication. The actual identity the web server uses depends on the platform and setup (usually IUSR_Foo).
To test out if this is the issue, try using temporarily replacing the connection string with one that uses SQL authentication. If this is the issue, you may want to either configure the web server to run the ASP under a different user account which has been granted database access (preferred) or give the current web server's identity access to the database. Or you can stick with SQL authentication, of course.
Either the server you're running the ASP on doesn't have a running database server, or the database server should have an instance name. One common mistake is to forget to add the SQLEXPRESS instance name for SQLExpress installs.

Resources