0

This is a problem that has plagued me for a while now. Let's say I create a Windows Forms C# application. Then I add a "Service Based Database" to the project. Then I create tables, populate them, yada yada. Then I connect the database to the project click on "project" then "add a new data source" etc... Then I get my connection string and add it to the code. I also changed the "Copy to Output Directory" property of the .mdf file to Copy if Newer even though I don't know what that means.

During run time, if I were to add data to the database, It will be saved. But if I open up a query file and directly search for that new data in the MDF file, it will not be there. Online research shows me that it's because a temporary database file is made whenever I build the app. How do I make it so that the data is copied into the .mdf file itself and not a temporary database file?

edit: this is the connection string:

<connectionStrings>
        <add name="myDB" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\constructionDatabase.mdf;Integrated Security=True"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
sami
  • 15
  • 5
  • You need to check in the mdf file which is copied in the bin directory. – Chetan May 04 '23 at 08:22
  • What is the connection string? A MDF file can be attached to the Server or not attached (localdb). The connection string string determines the Server, the instance of the Server, and if you are using a MDF file or a MDF file that is attached to the database. – jdweng May 04 '23 at 09:31
  • the connection string is SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myDB"].ToString()); – sami May 04 '23 at 09:41
  • @sami that is only the way you *get* the connection string, not the real one from the configuration. When you add *that* one to your post, feel free to hide private info – Hans Kesting May 04 '23 at 10:09
  • Ah, my apologies. I think this is it – sami May 04 '23 at 10:17
  • Related: https://stackoverflow.com/questions/13412154/attachdbfilename-is-not-working-after-deployment and https://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename – Hans Kesting May 04 '23 at 12:58
  • _I also changed the "Copy to Output Directory" property of the .mdf file to Copy if Newer even though I don't know what that means_: Seems that you probably should research this so that you understand what it means. – Tu deschizi eu inchid May 04 '23 at 14:10

0 Answers0