Attach Database to Project and View in Server Explorer

teymoorei

Active member
Joined
Jan 11, 2012
Messages
36
Programming Experience
1-3
Hello
I have copied a database file from another project into my project and added it to my project
with the command:
VB.NET:
    Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=" & My.Application.Info.DirectoryPath & "\dbkarkhaneh.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
I connect to it
In its properties, change "Copy to Output Directory" to "Copy If Newer"
I change
To display the data inside the database, I have to delete the database from my project every time and do the above path again.
What is the solution to avoid deleting the database and adding it again and doing "Copy to Output Directory" to "Copy If Newer" again?
I hope you understand what I mean.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,832
Location
Sydney, Australia
Programming Experience
10+
I don't understand what you mean. Here's how it is supposed to work. You add the data file to your project and that is your source file. You set Copy to Output Directory to Copy if Newer and then any changes you make while testing/debugging your project will be retained after each build unless you make a change to the schema or data in the source file. If you want to force a refresh of the working database, you simply delete the data file in the output folder and build. What is the actual problem you are encountering with that?

To view the source file in the Server Explorer, simply double-click it in the Solution Explorer. If you want view the working database, click the Connect to Database button in the Server Explorer, select the SQL Server Data File option and navigate to your working database. What is the actual problem?

By the way, you should change your connection string to use the "|DataDirectory|" place-holder:
VB.NET:
Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\dbkarkhaneh.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True")
Because you don't use code to create it, you can even store it in a config file or the like.
 

teymoorei

Active member
Joined
Jan 11, 2012
Messages
36
Programming Experience
1-3
I have to repeat all the steps above every time to see the changes I made from within my app, otherwise the changes won't apply to the database.
Of course, changes are applied to the main file that is inside the "debug" folder, but they are not displayed from within Visual Studio and Server Explorer, and I have to repeat the above steps every time to see them.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,832
Location
Sydney, Australia
Programming Experience
10+
changes are applied to the main file that is inside the "debug" folder, but they are not displayed from within Visual Studio and Server Explorer
Nor should they be. They are two different files so why would you expect changes to one to affect the other? The point of this is that your original source file is kept clean and clear of all the garbage you're going to be putting in it while testing. That means that, when it comes time to deploy your app, the source file is copied to the Release output folder and a clean database is deployed with your app. If it wasn't that way then you'd have to clean out all the test data from your database every time you wanted to deploy, or even when you wanted to star testing fresh again.

As I have already explained, if you want to see the working database in the Server Explorer then you simply need to add a connection to it in the Server Explorer.
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,832
Location
Sydney, Australia
Programming Experience
10+
Each time you build it breaks the connection in the Server Explorer, even if the working data file isn't overwritten. You simply select the data connection in the Server Explorer and click the Refresh button and it reconnects.
 
Top Bottom