0

I create simple example to create question easyer. So in my c# project I create an mdf database with articles. Then I connect database in my program and read values from table articles. It gives me results, but not the latest. If I have one result it showes me this one. Then I go in articles table to add one new article and run program again and in this case program showes me only the first one. But if I "Build solution" it finds all of them.

What I have to do? I wish, that program will have the latest result on startup.

SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");

try
{
     cn.Open();

     string sqlQuery = "SELECT * FROM Articles";
     SqlCommand sqlCommand = new SqlCommand(sqlQuery, cn);
     SqlDataReader sqlDataRead = sqlCommand.ExecuteReader();

     while (sqlDataRead.Read())
     {
         MessageBox.Show(Convert.ToString(sqlDataRead["ArticleLabel"]));
     }

     sqlDataRead.Close();
     sqlDataRead.Dispose();
     sqlCommand.Cancel();

     cn.Close();
 }
 catch (Exception) { MessageBox.Show("Database error!"); Application.Exit(); }
Clem
  • 11,334
  • 8
  • 34
  • 48

1 Answers1

0

If your MDF file has the property Copy To Output Directory set to Copy Always, then every time you build your application a fresh copy of the database file is copied from the project directory to the output Directory (BIN\DEBUG or BIN\RELEASE).
Of course this destroy any changes you have made in a previous run of your program

Change the property to Copy if Newer

Also, don't be fooled by what you see in the Server Manager Windows. The connections there could not point to the database in the Output directory but on the database in the Project Directory where you don't write anything.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • I tryed this, but still the same result's – Clem Mar 30 '13 at 10:54
  • Could you add the actual connection string to your question? – Steve Mar 30 '13 at 10:57
  • Well, the shortcut |DataDirectory| resolves to your BIN/DEBUG folder, so with Copy Always you destroy the previous runs. I stay with my answer, change to Copy if Newer or Copy Never, to be sure you don't destroy your adds at every run. If it fails again I will try to debug your Add Article code to check if the record is effectively inserted in the table. – Steve Mar 30 '13 at 11:08
  • I have now Copy if Newer, but still the same. My Add Article code? I add article directly in database table. The code pasted in question is all what i have in this example :) – Clem Mar 30 '13 at 11:11
  • How do you add the new record? Using the Server Explorer Window or Management Studio? – Steve Mar 30 '13 at 12:05
  • yes. Only for this example of course. I go in Server Explorer and manually add new row in table(in this case article). And when i run program there is no newest row...it appears only if i build solution before running..:/ dont know why – Clem Mar 30 '13 at 14:19