问题
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.
回答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.
回答2:
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.
来源:https://stackoverflow.com/questions/5215791/database-isnt-updating