Rejeev Divakaran
Rejeev Divakaran

Reputation: 4504

Accessing SQL Server from Console application

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");
        }
    }

Upvotes: 4

Views: 28688

Answers (5)

ssvinarenko
ssvinarenko

Reputation: 61

If you are running Vista, then this probably happens because Visual Web Developer runs under a privileged account, e.g. Administrator. This is done to allow you to develop and debug your apps. The Administrator account is included in the admins group on MSSQL. Your console application runs under your account which does not have permissions to connect to MSSQL.

Upvotes: 0

Binier
Binier

Reputation: 1107

It means your login has no rights to access that database.

Upvotes: 1

Pablo S G Pacheco
Pablo S G Pacheco

Reputation: 2600

Verify in app.config if the password is OK on attribute "connectionstring".

I did have the same problem you had. And my password was empty. I don´t know why

Upvotes: 1

ahains
ahains

Reputation: 1912

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?

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062840

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.

Upvotes: 3

Related Questions