المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : C# ADO.net Adapter Update Question.



C# Programming
11-26-2009, 01:57 AM
I'm new to C# and am trying to access and update a Access Database. I've managed to create the database and can see the results in Access. I'm also able to open the Database and load the tables, but when I try to do an update it does not post back my changes. Below is the code with comments where things seem to fail. I do not get an exception, I just don't see the changes in the database file.


// This method seems to work fine, it loads the data from the database into the six tables.
public void openDatabase(DataSet ds, string filename)
{
// I have 6 tables in the dataset/database.
string[] names = { "Project", "Portfolios", "Equations", "Scenarios", "Reports", "Rules" };
string strAccessConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename;

for (int i = 0; i < names.Length; i++)
{
string strAccessSelect = "SELECT * FROM " + names[i];

try
{
myAccessConn = new OleDbConnection(strAccessConn);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
return;
}

try
{
OleDbCommand myAccessCommand = new OleDbCommand(strAccessSelect, myAccessConn);
myDataAdapter = new OleDbDataAdapter(myAccessCommand);

myAccessConn.Open();
myDataAdapter.Fill(ds, names[i]);
}
catch (Exception ex)
{
Console.WriteLine("Error: Failed to retrieve the required data from the DataBase.\n{0}", ex.Message);
return;
}
finally
{
myAccessConn.Close();
}

}
}

// This is the method I'm having trouble with, I sent the Dataset off to another routine which changes some
// of the datarows, I can see the changes there in the dataset but update does not write them to disk.
public void updateDatabase(DataSet ds)
{
try
{
dw = new DebugWindow(); // This opens up a window to display the dataset
dw.Show();
dw.dumpDataSet(ds); // This dumps the dataset to the window, the dataset looks correct with my changes

myAccessConn.Open(); // I don't think I need to do an open as the update should do this but I'm trying all kinds of things
ds.AcceptChanges(); // Don't think I need this

// This seems to be the line that does not work. I've also tried (ds, "Rules") as well. Ideally, I would
// just pass in the dataset and all the tables would be updated.
myDataAdapter.Update(ds.Tables["Rules"]);
myAccessConn.Close();
}
catch (System.Exception ex)
{
dw.displayMsg("Update Failed" + ex.Message);
}
}