views:

912

answers:

3

I have a simple Console Application which connects to SQL Server database. However it throws the following error while running. Any clues?

Unhandled Exception: System.Data.SqlClient.SqlException: Cannot open database "Database" requested by the login. The login failed.
Login failed for user 'MYDOMAIN\MYUSERID'.
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(Boolean enlistOK)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, BooleanignoreSniOpenTimeout, Int64 timerExpire, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(String host, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, Int64 timerStart)

I use SQL Server 2005 Express Edition. However I am able to connect to SQL Server from Visual Web developer using Database Explorer. The code I used is given below:

using System;
using System.Data;
using System.Data.SqlClient;

    public class Test
    {
        public Test()
        {

        }
        static void Main()
        {
            Console.WriteLine("hello");
            string connectionString = "Data Source=localhost\\SQLEXPRESS;Database=Database;Integrated Security=true";
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            Console.WriteLine("done");
        }
    }
+2  A: 

Console apps can talk happily to SQLExpress databases. I expect you simply need to configure access to your domain account via management studio. Presumably, the web-developer app is using the ASPNET account, which has different domain credentials.

Marc Gravell
Is there any workaround? Installing new software has it's delay (corporate network). How can I use ASPNET account instead of Integrated security?
Rejeev Divakaran
A: 

If you running Vista, than this probably happens because Visual Web Developer runs under priveleged account e.g. Administrator (to allow you to develop and debug your apps). Administrator included to admins group on MSSQL. And you console application runs under your account which does not have permissions to connect to MSSQL.

ssvinarenko
I run windows XP.
Rejeev Divakaran
A: 

I would guess you may be using a different protocol between the web dev and your .net app. As a quick test, you can launch Sql Server Configuration Manager and enable each protocol under the network configuration. You can also check/change the client protocols for sql native client in that config tool. They should both have shared memory enabled by default, but doesn't hurt to double check.

From http://msdn.microsoft.com/en-us/library/ms345154.aspx:

Networking Support

Only the shared memory connection type on the local machine is accessible by default for SQL Server Express, although the user can explicitly turn on other supported protocols such as TCP/IP and Named Pipes

I'm assuming that this is a local install of sql server, not over the network right?

Yes, console app and SQL Server are running on the same machine.
Rejeev Divakaran