2

I am trying to write some data into a .MDF database that has only one table, and it's not updating after I execute this part of the code:

        DatabaseDataSet.MyTableRow newRow;
        newRow = databaseDataSet.MyTable.NewMyTableRow();
        newRow.name = "x";
        newRow.level = 100;
        newRow.health = 100;
        newRow.weapon = "club";
        this.databaseDataSet.MyTable.Rows.Add(newRow);

        int result = MyTableTableAdapter.Update(databaseDataSet.MyTable);
        MessageBox.Show(result.ToString());

I am new to working with SQL databases in C#.

I'm using TableAdapter.Update to update the MyTable table in the database, I even tried writing my own query version of Update, and I also tried with the MyTableAdapter.insert function...and no results. I can successfully read data from a database, but when it comes to writing, I fail hard.

Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
deckard cain
  • 497
  • 10
  • 24
  • can you double check your MDF file (or the folder containing the file) has write permission? – Raptor Mar 07 '11 at 04:22
  • Did you call SaveChanges() or sth like that? – vissi Mar 07 '11 at 05:11
  • I've checked, none of mdfs are read only, which should mean i have the permission to change them. Also, i didn't call SaveChanges() nor anything like that. Update should make the changes in the database alone i think. – deckard cain Mar 07 '11 at 13:26
  • How is it that you know you definitely aren't writing to the database? – Task Apr 27 '11 at 15:29
  • i actually solved it back then when i asked..kind of... It updated the copy of the database. If i started the program from debug folder it worked as a charm, but from visual studio it didnt show. I guess i didnt connect well to the database or something. Afterwards i think i connected to the database via connection string. – deckard cain Apr 30 '11 at 23:37

2 Answers2

1

Update (insert and delete) will only modify the local client view of the table adapter, you have to SaveChanges in order to commit it to the database.

Matthew
  • 24,703
  • 9
  • 76
  • 110
0

I would hazard a guess that you're using a compact SQL database in your project?
That means that a database file will be created wherever the program is running.
So if you run in Debug mode, you get a database in your bin/Debug folder. Run it in Release mode and you're working on a totally different database in your bin/Release folder. Run the installed version and it's a different database file again.

I found that out pretty quickly when I built my first .net program with a compact SQL database, the important thing to know is that the entire database is in a single file that is usually in the same directory as the running program. Keep that fact in mind at all times and a lot of little issues like this won't bite you.

Task
  • 3,668
  • 1
  • 21
  • 32