2

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?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
Nemi
  • 19
  • 3
  • You can try to use `SqlConnectionStringBuilder` – Niewidzialny Apr 04 '17 at 06:44
  • Have you tried setting `Data Source =` to your SQL Server 2008 server name like for example `Demo-My-DSK024\SQLEXPRESS` or something which can be found by right clicking server name and on the generals tab? – P. Pat Apr 04 '17 at 06:45
  • As you are using VS 2012 it should be easy to open "View -> Server Explorer" right click on "Data Connections" -> Add Connection. The following dialog will help you to connect. The connection string can be copied from the newly created connection via "Properties" (if you need it) – Shnugo Apr 04 '17 at 06:45
  • 2
    See also: [connectionstrings.com](https://www.connectionstrings.com/) – Corak Apr 04 '17 at 06:49
  • No I did not, As I have said i'm new to this, please tell me how i can do it? – Nemi Apr 04 '17 at 06:54

3 Answers3

3

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>
trashr0x
  • 6,457
  • 2
  • 29
  • 39
Dhananjay Singh
  • 161
  • 1
  • 12
  • thank you for answering but my connection string is used for a windows for application and from where i know the C# code's your code is in XAML language and it's can be used in WPF if i am right. So Please make it in C# and i will apply it to my project and hope it works. thank you – Nemi Apr 04 '17 at 07:02
  • @Nemi - This post shows you how the connection string should be declared in the app/web config file. See [Store Connection String in Web.config](https://www.connectionstrings.com/store-connection-string-in-webconfig/) for more information. -- "It is a good practice to store the connection string for your application in a config file rather than as a hard coded string in your code." – Corak Apr 04 '17 at 07:04
  • @Corak correction, it is `app.config` and not `web.config`. Kindly see post tags and post content before suggesting links that may relatively help OP but may also confuse them. – P. Pat Apr 04 '17 at 07:06
  • @P.Pat - Yes, the link only talks about `web.config`, but for connection strings there is no difference between how they are declared in `web.config` or `app.config`. I tried to make that point with "app/web config", but will be more direct next time. – Corak Apr 04 '17 at 07:12
  • See also: [Adding and reading from a Config file](http://stackoverflow.com/questions/10864755/adding-and-reading-from-a-config-file) and [What is App.config in C#.NET? How to use it?](http://stackoverflow.com/questions/13043530/what-is-app-config-in-c-net-how-to-use-it) for further information. – Corak Apr 04 '17 at 07:17
  • @Nemi : On the page where u want to use this connection string write this code: `String Conn = System.Configuration.ConfigurationManager. ConnectionStrings["connectionStringName"].ConnectionString;` – Dhananjay Singh Apr 04 '17 at 07:25
0

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.

caesay
  • 16,932
  • 15
  • 95
  • 160
0

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

Nemi
  • 19
  • 3