开发者

The new row added didn’t save into underlying database using C# and SQL server

I am trying this code to add new row to database, the new row is saved but when I close the program down, reopen it, the row has disappeared.

private void Dep_Add_Click(object sender, EventArgs e)
{
   try
   {
       SqlCommandBuilder cb = new SqlCommandBuilder(Dep_da);

       DataRow dRow = Dep_ds.Tabl开发者_开发技巧es["Department"].NewRow();
       dRow[1] = Dep_Name.Text;
       Dep_ds.Tables["Department"].Rows.Add(dRow);

       Dep_MaxRows = Dep_MaxRows + 1;
       Dep_inc = Dep_MaxRows - 1;

       Dep_da.Update(Dep_ds,"Department");
   }
   catch (Exception exceptionObject)
   {
      MessageBox.Show(exceptionObject.Message);
   }

So I hope someone can help me


You don't give much context information, so this is a bit of a guess...

If you are doing this from inside Visual Studio, and are writing to a local (attached) database, then every time you execute the program, the local db is being copied from your project to your bin folder, so you are starting each execution with the same copy of the database, overwriting the db that was modified in the last execution.

You should be able to test if this is the case by running your exe from explorer or the command line.


You already asked this question here, though with slightly different code.

I gave an answer for you on your previous question - which is to use the SqlDataAdapter.Update command. You're doing that here, but you didn't post the rest of your code so I don't know if you changed anything.


You would probably call DataSet.Tables[0].AcceptChanges() method?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜