User Instance in a connection string gives an error - asp.net

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.

Related

Using named instance in connection string

I want to upload my DNN site in customer's host, they use SQL Server 2005 and they use named instance, how can I use named space in connection string ?!
<add key="SiteSqlServer"
value="Data Source=79.175.164.226,2005; Initial Catalog=jdmedu; User ID=; Password=;" />
DNN shows database connection error.
Can you check that you can construct ODBC Connections to the DB (outside of your application).
I Suspected that, the problem is in the Configuration of the DB Instance and it can be solved from the SQL Server Configuration Manager.
Also, can you check
http://www.connectionstrings.com/sqlconnection/connect-via-an-ip-address/
Data Source=190.190.200.100,1433;Network Library=DBMSSOCN;
Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;

dynamics AX batch job through ODBC

I have problems with an ODBC connection used in a batch job: when executed, I have the following error message :
"The ODBC operation failed.Unable to connect to database
Invalid connection String attribute Trusted_Connection
Unabe to create object 'OdbcConnection' "
I try to connect to an Access database, I use the following code:
LoginProperty loginProperty;
OdbcConnection odbcConnection;
;
loginProperty = new LoginProperty();
loginProperty.setDSN("MyDSN");
loginProperty.setDatabase("MyDatabase");
odbcConnection = new OdbcConnection(loginProperty);
when debugging, it crashes on odbcConnection = new OdbcConnection(loginProperty). I tested the same code in a job in local client execution, there was no problem and it worked fine(without defining login password in code or DSN). My DSN seems to be OK on AOS, I'm using AX 2009 without SP1, I saw some examples where there was login/password defined in loginProperty object , the setUserName() and setPassword() methods are not available in my AX version loginProperty object. My class is executed on server-side (as any batch job).
Is there some particular setup to do to connect to ODBC datasource through batch job?
any ideas or suggestions?
thanks for your help
Edit:AOS is running on Windows server 2003 32 bits, ODBC tested on it, works fine but doesn't work in AX through batch job
Most likely you will need a 32-bit DSN connection for your AX. I would create both versions on the AOS server.
Look here for details.
It may be caused due to permisions. "trusted connection" refers to the fact that you connect to the database through the user logged to the current session. When you run a Job from the AX client, this is executed with the user that runs the current AX client (the one logged to the active session); When code is executed from server in a batch job, code is executed through the user that runs the AOS windows service.
Has this user permissions to read/write/access the access file and directory?
If you are on a 64 bit machine use C:\Windows\SysWOW64\odbca32.exe to create the datasource.
Check under which ID the AOS runs, log on with that ID and make sure you can 'see' the data source.
Also make sure that the AOS account has access to the directory/files (if using file-based odbc)

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

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

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