2

I have seen the other posts on this subject. Yet so far there has been no solution. I am working with Visual Studio 2013 in C#.
I have a database "Database1.mdf" with one table called Customers, which just has two records. I created the DataSet named CustomersDataSet (Menu: Project, Add New Data Source...) based upon this database.
This is my code.

CustomersDataSetTableAdapters.CustomersTableAdapter cta = new CustomersDataSetTableAdapters.CustomersTableAdapter();

        CustomersDataSet ds = new CustomersDataSet();

  // Fill our customersDataSetTable with the data from customers adapter
        cta.Fill(ds.Customers);

        Console.WriteLine("BEFORE");
        foreach (CustomersDataSet.CustomersRow customer in ds.Customers.Rows)
        {
            Console.WriteLine(customer.FirstName + " " + customer.LastName);
        }



        Console.WriteLine("\nMaking changes now...");

        // Insert a new record
        CustomersDataSet.CustomersRow newCustomer = ds.Customers.NewCustomersRow();
        newCustomer.FirstName = "Brian";
        newCustomer.LastName = "Faley";
        newCustomer.City = "Denver";
        newCustomer.State = "CO";


        ds.Customers.AddCustomersRow(newCustomer);

        // Update a record, [0] = gets access to the first row of the customers table
        ds.Customers[0].FirstName = "Robert";

        // Delete a record
        ds.Customers[1].Delete();

        // Update the dataset ds. Commit changes to the database
        cta.Update(ds);

        Console.WriteLine("\nAFTER");
        foreach (CustomersDataSet.CustomersRow customer in ds.Customers.Rows)
        {
            Console.WriteLine(customer.FirstName + " " + customer.LastName);
        }

It works insofar as I do see the changes made to the dataset after "AFTER".
Yet I can run it as often as I wish - never are the changes written to the underlying database. The Update should do just that, but it does not. There is no AcceptChanges() in my code. I have followed up on all these suggestions - they do not lead anywhere.
Would someone have an idea?
I googled far and wide and all posts on this issue are unsolved.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Andi Truman
  • 111
  • 1
  • 6

1 Answers1

2

When you debug the application the mdf file is copied to the bin\debug folder and your changes are committed to the database there.

Every time you start the project the mdf in the debug folder gets overwritten with the original database.

You can stop this behavior by going to the database settings in your solution and set the database to copy only if your version is newer.

Chances are your code was working all along.

Hope this helps.

Marc van Nieuwenhuijzen
  • 1,637
  • 1
  • 14
  • 22