Results 1 to 9 of 9

Thread: [RESOLVED] Can't connect to SQL Server Express 2008 database

  1. #1

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Resolved [RESOLVED] Can't connect to SQL Server Express 2008 database

    When trying to connect to a SQL Server Express 2008 database, I receive the following error:

    Failed to generate a user instance of SQL Server. Only an integrated connection can generate a user instance. The connection will be closed.
    Which occurs on my "connection" line:

    VB.NET Code:
    1. Dim connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=dbISTme!.mdf;Integrated Security=False;User ID=test;Password=test1;User Instance=True")
    2.  
    3.         Dim command As New SqlCommand("SELECT COUNT(*) FROM Credentials WHERE UserID = @UserID AND Password = @Password", _
    4.                               connection)
    5.  
    6.         With command.Parameters
    7.             .AddWithValue("@UserID", Me.txtUserName.Text)
    8.             .AddWithValue("@Password", Me.txtPasssword.Text)
    9.         End With
    10.  
    11.         connection.Open()
    12.  
    13.         If CInt(command.ExecuteScalar()) = 0 Then
    14.            
    15.         Else
    16.             Me.DialogResult = Windows.Forms.DialogResult.OK
    17.         End If
    18.  
    19.         connection.Close()]

    I've worked with SQL CE databases in the past and I've never had a problem with them. The connection strings are a little bit different, so I think that might have something to do with it.

    Originally, I looked up that error and got a whole bunch of nonsense. I found two solutions that worked for most people, but they did not work for me. The fist was an issue with XP and SP2, which I'm Windows 7. The second resolution was to delete the SQLEXPRESS folder.

    Then I saw a post from another site:

    One thing to remember is that although user instances is the default when working with Visual Studio, it is not mandatory to use them, it is quite possible to attach to databases in the traditional way from SQL Express, to do this you just need to edit the connection string and delete the "user Instance=true". User Instances just make it easier (when it does work) to work with databases as though they were files.
    So, I deleted that line and got another error:

    An attempt to attach an auto-named database for file dbISTme!.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share.
    This seems like the most generic error ever. There's even less information about this error.

    I've made sure that the database uses the SQL authentication and not Windows and made sure my user, "test", has the correct permission.

    Anyone have any on ideas on what I'm doing wrong?

    Thanks
    Last edited by weirddemon; Jun 29th, 2010 at 09:23 PM.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Can't connect to SQL Server Express 2008 database

    The problem is exactly what the error is telling you... you are trying to attach the mdf file in a user instance, but at the same time you told it to not use integrated security, but SQL security... which you can't do... IF you are in fact trying to attach the database in a user instance, you MUST use integrated security.

    The second error (about the db already existing) happened because quite literaly, the DB already does exist.

    Sounds like you'r trying to connect to a database that already exists on the sql server ... if that's the case, then just connect to it... don't specify a user instance and don't give a path to the mdf... just use a normal connection straight up.


    As a side note, where did you find that quote? I've seen that more than once now, and I don't think it's accurate information (regarding user instances being the default).

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Can't connect to SQL Server Express 2008 database

    Alright. I didn't realize that I was trying to attach it each time than just connect to it

    I changed my connection string to:

    SQL Code:
    1. "Data Source=.\SQLEXPRESS;Initial Catalog=dbISTme!.mdf;User ID=test;Password=test1;"

    Since I no longer need to attach the database, I assume it's okay just to remove the Integrated Security line. I no longer receive either issue once I remove those two lines and just connect to the DB.

    However, I'm getting another issue:

    Cannot open database "test.mdf" requested by the login. The login failed. Login failed for user 'test'.
    Is there a specific way that the user needs to be setup? I made sure the credentials matched up.

    I setup the user with these settings:



    Uploaded with ImageShack.us

    And I got that quote from here.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Can't connect to SQL Server Express 2008 database

    you';re specifying the filename.... don't... just the name of the database.... dblSTme! .... what's with the ! in the name?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Can't connect to SQL Server Express 2008 database

    Quote Originally Posted by techgnome View Post
    you';re specifying the filename.... don't... just the name of the database.... dblSTme! .... what's with the ! in the name?

    -tg
    When I remove the extension, I receive the same error. The exclamation point is part of the database name. It's part of the app name as well, so I thought to just include it. I suppose it's not really needed either way... but yeah
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Can't connect to SQL Server Express 2008 database

    huh... well, that would imply that the user name or password is wrong.... are you *sure* the password is right?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: Can't connect to SQL Server Express 2008 database

    Quote Originally Posted by techgnome View Post
    huh... well, that would imply that the user name or password is wrong.... are you *sure* the password is right?

    -tg
    Eureka! The password was correct, but it turns out that I needed to map the database to the user. I did not know that. I just started playing around with the settings and figured it out.

    Thanks for your help, tg!





    Uploaded with ImageShack.us
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Can't connect to SQL Server Express 2008 database

    No you don't have to map the database to the user you grant the user permission on the database. You do that by adding the login to the database security tab.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: [RESOLVED] Can't connect to SQL Server Express 2008 database

    Quote Originally Posted by GaryMazzone View Post
    No you don't have to map the database to the user you grant the user permission on the database. You do that by adding the login to the database security tab.
    Ah. Well... that would make sense too. I escalated the user's permission and mapped the database at the same time and attributed the resolution to the mapping. I suppose that's a faulty syllogism

    Thanks for clearing that up, Gary
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

Posting Permissions

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



Click Here to Expand Forum to Full Width