2

In my c # project that I work with local db, I need to clear the table. I can view the table that I cleared while the program is running, but when I restart it, it returns to its original state.However, I do not experience it problem when adding to my Sql table.i dont understand.Is there anyone who can help me?

 SqlConnection SqlConn = new SqlConnection(@"Data Source = (Localdb)\MSSQLLocalDB; AttachDbFilename=" +
                                              Directory.GetCurrentDirectory() + @"\HtbTs.mdf;" +
                                              "Integrated Security=SSPI;Connect Timeout=30;User Instance=False");
SqlConn.Open();
        string Delete = "DELETE FROM BagenTablo";
        SqlCommand komut = new SqlCommand(Delete, SqlConn);

        komut.ExecuteNonQuery();
        SqlConn.Close();
  • at what point exactly the table is filled again? may be some code of yours populates the table? – AgentFire Jul 23 '19 at 15:29
  • Why are you connecting directly to the datafile instead of the database? There are many ways this can go wrong. Here is an article that is similar to your problem. https://www.sentryone.com/blog/aaronbertrand/bad-habits-attachdbfilename – Sean Lange Jul 23 '19 at 15:31
  • 3
    Do you have the MDF file listed in your project files? If yes click on it and tell us what is the value for the property _Copy To Output Directory_ – Steve Jul 23 '19 at 15:32
  • thank you a lot. i solved thanks to [this answer](https://stackoverflow.com/a/17147460/11825515) – Rasim Cekic Jul 25 '19 at 08:14

1 Answers1

1

If the original db in your project has a data so every time when you build your project or debug it the visual studio copy the original db file into output folder

you can open MDF file in your project and right click on db in server window, then click New Query
type and execute this query
TRUNCATE TABLE BagenTablo

Almelyan
  • 37
  • 10