开发者

How to Update Data Source in C#

I am working on a Windows Form-Based application in Visual Studio 2008 using C# with an Access database as a back end. I added the Access database into the project by going to Data->Add New Data Source and I am able to retrieve data from the database's custom-made DataSet perfectly.

My next goal is to save new data back into the Access database, creating a new row. For the life of me, however, I can't seem to figure out how to do this. It looks like I need either an OLEDbConnection to the database or a TableAdapter hooked up to the database, but I don't seem to have either one of those. I'd think they would be premade or something when I added the new data source, just like the DataSet was, but if that's the case I can't figure out how to reference them.

开发者_如何学GoWhat is the best way to add data back into a data source that's been added to the project?


You probably have a (configured) TableAdapter, it is in a sub-namespace with your (generated) Dataset.

Somewhere in your code there has to be an Adapter.Fill(table), find it.
Copy it and change it to Adapter.Update(table) to write back to the database.

Instead of talking you through the multiple other ways of adding a record, have a look at the videos here


This is how I ended up solving it: I had to create a new table adapter, create a new row, and then update the table adapter with it.

// Set up the table adapter and new row
MyDataSetTableAdapters.EmployeeTableAdapter adapter;
adapter = new MyDataSetTableAdapters.EmployeeTableAdapter();
MyDataSet.EmployeeDataTable table = adapter.GetData();
MyDataSet.EmployeeRow newRow;
newRow = table.NewEmployeeRow();

// Insert the values of the fields into the new row here

// Save the row
table.AddEmployeeRow(newRow);
adapter.Update(table);

Thanks for the help!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜