3

After spending 6 hours today to get a connection with my SQL Server 2012 database from Visual Studios 2012, I give up.

Setup:

  • Database: App_Data/GameDB.mdf
  • DB Location: C:\Users\USERNAME\Documents\Visual Studio 2012\Projects\ProjectRawWar\ProjectRawWar\App_Data\GameDB.mdf
  • Connection string:

    <add name="LocalSqlServer" 
         connectionString="Data Source=(LocalDB)\v11.0;Initial Catalog=GameDB;Integrated Security=False;"/>
    
  • I first tried a SQL Server Compact database, didn't work.

  • I tried many other connection strings from connectionstrings.com, didn't work.
  • I tried to put the full path of the database in the connection string, didn't work.

Can someone please help me out of my misery?

Update: I just followed http://msdn.microsoft.com/en-us/library/aa983322.aspx. The database is there. I copied the connection string from the wizard right into my webconfig connectionstring, still doesnt work. Why? Where is the logic?

6
  • 1
    Try this easy way to get connection strings. Commented Oct 14, 2012 at 15:36
  • I tried that too. I tried server = localhost -> didnt find any database. I tried server = computername -> didnt find any database. Commented Oct 14, 2012 at 16:10
  • Can you connect to it using Sql Server Management Studio? Commented Oct 14, 2012 at 16:22
  • Is your SQL Server Browser Service running? Commented Oct 14, 2012 at 16:23
  • What error do you get when you try to connect? Commented Oct 14, 2012 at 16:38

3 Answers 3

1

Reading your question I am getting the feeling that you are trying to connect to a database file. While you can do that with Access "Databases" it is not possible with SQL Server databases. The file needs to be mounted or attached to SQL Server first.

Check out this MSDN article for details: http://msdn.microsoft.com/en-us/library/ms190209.aspx#SSMSProcedure

Sign up to request clarification or add additional context in comments.

3 Comments

I'll take a look at that, thanks. The SQL Server Database is attached to my project tough, as I can open it in Visual Studios.
'<add name="DataContext" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|GameDB.mdf;User Instance=true" />' is currently my connectionstring. It looks like it's connecting cause now i'm getting error: 'Cannot open database "GameDB" requested by the login. The login failed. Login failed for user HPCDSBE\Wesley.DeKeirsmaeker. ' In Visual Studio my database is attached and I can work with it.
I downloaded and installed MS SQL Server Management and also this program can't connect to my Visual Studio server. Error: ` A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) (Microsoft SQL Server, Error: 2)`
0

If you're going to disable integrated security, then you need to provide a username and password.

IE:

Server=127.0.0.1;User ID=useridhere;Password=passwordhere;database=dbnamehere

Be sure to make sure the userid and password you specify has access to the database you need. For testing, you can use the 'sa' account, but that's a poor long-term solution for major security reasons.

2 Comments

'<add name="DataContext" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|GameDB.mdf;User Instance=true" />' is currently my connectionstring. It looks like it's connecting cause now i'm getting error: 'Cannot open database "GameDB" requested by the login. The login failed. Login failed for user HPCDSBE\Wesley.DeKeirsmaeker. ' In Visual Studio my database is attached and I can work with it.
You need to interact with whatever the database is attached to, not the MDF file.
0

Ok I fixed it myself. How I did it: Delete every .sdf and .mdf file. Just made following connection strings:

    `<add name="DataContext" 
     connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\GameDB1.mdf;Integrated Security=True"
     providerName="System.Data.SqlClient" />
<add name="LocalSqlServer"
     connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\GameDB1.mdf;Integrated Security=True"
     providerName="System.Data.SqlClient" />`

Visual Studio created my database using my DBContext.

Thanks all for the information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.