Nemi
Nemi

Reputation: 19

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?

Upvotes: 2

Views: 649

Answers (3)

Dhananjay Singh
Dhananjay Singh

Reputation: 161

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>

Upvotes: 3

Nemi
Nemi

Reputation: 19

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

Upvotes: 0

caesay
caesay

Reputation: 17233

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.

Upvotes: 0

Related Questions