"login failed for user..." when trying top open a local .mdf database
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: "login failed for user..." when trying top open a local .mdf database

  1. #1
    Join Date
    Feb 2012
    Posts
    10

    "login failed for user..." when trying top open a local .mdf database

    this is a question that's been asked numerous times, but I've spent (literally) several hours trawling the answers here and elsewhere on the net with no joy.


    I have my database set up with tables, columns etc (not populated yet) in a new project. I have set up the open using the code...

    Code:
                SqlConnection dataConnection = new SqlConnection();
                SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
                builder.DataSource = ".\\SQLExpress";
                builder.InitialCatalog = "FantasyDatabase";
                builder.IntegratedSecurity = true;
    
                dataConnection.ConnectionString = builder.ConnectionString;
                MessageBox.Show(dataConnection.ConnectionString);
    
               
                //try
                //{
                    dataConnection.Open();
                //}
                //catch
                //{
                    MessageBox.Show("could not open database");
                    this.Close();
                //}
    ...with the try/catch commented out so I can see the full exception

    It is...
    Code:
    System.Data.SqlClient.SqlException (0x80131904): Cannot open database "FantasyDatabase" requested by the login. The login failed.
    Login failed for user 'Richard-PC\Richard'.
       at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
       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, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
       at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
       at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
       at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
       at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
       at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
       at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
       at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
       at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
       at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
       at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
       at System.Data.SqlClient.SqlConnection.Open()
       at FantasyLeague.LoadCountryDatabase.button1_Click(Object sender, EventArgs e) in C:\Users\Richard\documents\visual studio 2010\Projects\FantasyLeague\Form1.cs:line 36
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    The connection string, as shown by my first messagebox is "Data Source=.\SQLExpress;Initial Catalog=FantasyDatabase;Integrated Security=True"

    the security properties for the database do allow full control for "Richard-PC\Richard"

    I'm using windows authentication, and if go to the " add connection" window from databases explorer, and browse to my database, it comes back as connection successful.


    Other suggestions have involved Server Management Studio. I didn't have that, so I installed it, but I don't really have any idea how to use it.

    It been suggested to check that the database is connected through management studio, but I can't find much (it just shows some system databases that I don't recognise). If my database isn't connected, I can't see how you would connect to it.


    Any help appreciated as I've never set up a database before, and I really have no what is going wrong.

  2. #2
    Join Date
    Feb 2012
    Posts
    10

    Re: "login failed for user..." when trying top open a local .mdf database

    for additional info (no idea if relevant or not) I found the "attach" window in server management studio, but it did not let me navigate to my database

    It got to users\Richard, then the tree just ended, as seen below


  3. #3
    Join Date
    Feb 2012
    Posts
    10

    Re: "login failed for user..." when trying top open a local .mdf database

    since my second post I've found more suggestions, which say to copy the database from the orginal location to C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\DATA

    from doing that, I am able to select it and attach it.



    It still doesn't make a blind bit of difference though. Same error as before.



    If nobody can help, is there actually anywhere that gives help professionally, as it's absolutely vital that I get this to work.

    Alternatively, is there an easier way to just set up an sql database for use solely within one application with no external users? It all seems horrifically complex and non-intuitive for what I'm trying to achieve.
    Last edited by Richardl1969; February 9th, 2012 at 06:42 AM.

  4. #4
    Join Date
    Feb 2012
    Posts
    10

    Re: "login failed for user..." when trying top open a local .mdf database

    OK.....I was getting nowhere with this, so I started a brand new project, this time using a local .sdf database, dug around on the net to find some code on how to open for an sdf database, and it seems to have worked. Well, it opens anyway.



    As I now have something to work with, the problem is resolved by finding a completely different way of doing it.

    I am still intrigued by the initial problem though, but after one and half days of hitting my head against a brick wall, I'm not going to look at it any further, so I owuldn't expect anyone else to either.


    I've not marked it resolved as there hasn't been an actual solution to the problem, other than starting from scratch with a different kind of database.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center