3
votes

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?

3
I tried that too. I tried server = localhost -> didnt find any database. I tried server = computername -> didnt find any database. - Wesley De Keirsmaeker
Can you connect to it using Sql Server Management Studio? - VahidNaderi
Is your SQL Server Browser Service running? - Adam Wenger
What error do you get when you try to connect? - Cristian Lupascu

3 Answers

1
votes

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

0
votes

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.

0
votes

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.