'Connectionstring: is it the right form?

I'm new to SQL Database C# projects and I have a problem with connecting to my database in a windows form application which is a single user app and I want to use it just for myself. I'm using Visual Studio 2012 On windows7 64-bit and have SQL Server 2008 and 2012 Installed on my DELL Inspiron15 3521 Laptop.

First, I don't know if my connection string is correct or in a correct form. this is my connection string:

con.ConnectionString = "Data Source=(local); Initial Catalog=Library;Integrated Security=True";

Secondly, I have another problem: when this code is running in Visual Studio 2012, it's telling me that "Cannot open database "Library" requested by the login. The login failed.". This is happening when my authentication in SQL server is Windows Authentication and I don't have any login information add to the database. Is any configuration for it or it's just my code that is wrong?



Solution 1:[1]

Your connection string should be like this:

<connectionStrings>    
    <add name="ConnStringDb1" 
         connectionString="Data Source=localhost\SQLSERVER;Initial Catalog=Library;Integrated Security=True;" 
         providerName="System.Data.SqlClient" /> 
</connectionStrings>

Solution 2:[2]

If you're looking to use the testing / local database that gets installed with VS you're looking for (localdb)\\mssqllocaldb

So the connection string would be then:

Data Source=(localdb)\\mssqllocaldb; Initial Catalog=Library;Integrated Security=True

You can store it in the app.config like the other answer, otherwise it might depend on what you're using.

Initial Catalog is talking about the name of the database that will be created/used
Integrated Security means it will authenticate to the database with the current windows user

You need both of those things in order to connect to the built-in localdb.

Solution 3:[3]

thank you all for all the responses but I have found my answer in @Corak s answer, the website which is in the comment. however this is the right form of the code that I should used for my string connection:

@"Data Source=(LocalDB)\v11.0; AttachDbFileName=|DataDirectory|\Library.mdf; Integrated Security=True";

In my case the |DataDirectory| is the path in my computer for the Database(Library.mdf) so if anyone used this code instead of |DataDirectory| write the path Like the example because if you use |DataDirectory| The code will run but nothing in database will happen (or at least for me it didn't work). Here is the example:

@"Data Source=(LocalDB)\v11.0;AttachDbFileName=AttachDbFileName=C:\Users\John\Documents\Visual Studio 2012\Projects\MyDatabasProject\MyDatabasProject\Library.mdf; Integrated Security=True";

My sqlserver is VS2012 local database server v11.0 and that was the thing I had to write in my code specifically.

Hope it's useful

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 trashr0x
Solution 2 caesay
Solution 3 Nemi