You can get the connection through the window Server Explorer.
In Server Explorer, click on Connect to database. Then choose the option Microsoft SQL Server Database File (Sqlclient) and then select the . mdf file you want. Fill in the necessary steps.
After adding the database that will appear in Data Connections inside the Server Explorer, right-click on the Data Connection and finally on Properties.
Inside the window Properties, there is a property called Connection String and that’s the connection you have to use to access the database.
I recommend placing this connection in a Resource.
I still can’t do it, you’re making a mistake on the bars:
– Tiago Luca
The code is: Sqlconnection connection = new Sqlconnection("Data Source=(Localdb) Mssqllocaldb;Attachdbfilename= C: Users Tiago Documents Visual Studio 2017 Projects Projects Final Work POO Final Work POO BD.mdf";Integrated Security=True;Connect Timeout=30);
– Tiago Luca
Try putting a @ before the string. That is: @"Data Source=(Localdb) Mssqllocaldb;Attachdbfilename= C: Users Tiago Documents Visual Studio 2017 Projects Projects Final Work POO Final Work POO BD.mdf"
– Joel Vicente
If it doesn’t work it puts a second backlash on each backlash: "Data Source=(Localdb) Mssqllocaldb;Attachdbfilename= C: Users Tiago Documents Visual Studio 2017 Projects Projects Final Work POO Final Work POO BD.mdf";Integrated Security=True;Timeconnect Out=30);
– Joel Vicente
This is because in C# backlash is an escape character in strings. To use backlash in strings use one of these two solutions.
– Joel Vicente
using the connection string I came across the error: "An Attempt to attach an auto-named database for file C: Users John Documents visual studio 2010 Projects PAS bin Debug//Pataddsys.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is Located on UNC share." Obs: the path is fictitious.
– Tiago Luca