Hi,
DataSets are "disconnect ed" from the data source that was used to fill them.
In other words, they are only in-memory representations of the data in your
data store.
To update the data store with the changes in the DataSet you can call the
Update method on your data adapter and pass it the DataSet.
In order for the data adapter to successfully update the database, you must
supply the command for the update.
As Cor mentioned, you should really construct "con" in a C# "using" statement
to ensure that the connection is closed in the case of an exception being
thrown. You don't want to leave connections open. (Especially not in Access,
which I believe is really restrictive in the amount of concurrency it
supports):
DataSet ds = new DataSet();
using (OleDbConnectio n conn = new OleDbConnection (
testacc.Propert ies.Settings.De fault.mytestCon nectionString))
{
using (OleDbDataAdapt er adp = new OleDbDataAdapte r(
"SELECT * FROM who", conn))
{
// The following line isn't normally required.
// adp.Fill opens the connection and closes it before returning,
// but since we want it to remain open for the next
// operation (Update) we'll open it now.
// adp.Fill won't close it in this case.
conn.Open();
adp.Fill(ds, "who");
DataRow row = ds.Tables["who"].NewRow();
row["name"] = "Joe";
row["address"] = "1 csharp road :)";
row["age"] = 25;
ds.Tables[0].Rows.Add(row);
// in order to update the database we must tell the adpater how:
using (OleDbCommand comm = new OleDbCommand(
"INSERT INTO who (name, address, age) VALUES (pName, pAddress,
pAge)", conn))
{
comm.Parameters .Add("pName", OleDbType.VarCh ar).SourceColum n =
"name";
comm.Parameters .Add("pAddress" , OleDbType.VarCh ar).SourceColum n =
"address";
comm.Parameters .Add("pAge", OleDbType.Numer ic).SourceColum n =
"age";
// because we have added a new row we must set the InsertCommand
adp.InsertComma nd = comm;
// update the database
adp.Update(ds, "who");
}
}
}
--
Dave Sexton
"SLIMSHIM" <SL******@discu ssions.microsof t.comwrote in message
news:EE******** *************** ***********@mic rosoft.com...
Hi,
I"m new to c# and .net. I wrote a small program to add rows to an access
table.
the program goes thru the motions but the data never gets there.
here is my code. I am intentionaly not using the form controls. I"m trying
to do it solely thru code. You can safely assume I have an access datsabase
with one table called who with three columns(fields) .
where did i go wrong ?
OleDbConnection con;
OleDbDataAdapte r dbad;
DataSet ds;
DataRow dr;
con = new
OleDbConnection (testacc.Proper ties.Settings.D efault.mytestCo nnectionString) ;
dbad = new OleDbDataAdapte r("select * from who",con);
ds = new DataSet();
dbad.Fill(ds, "who");
dr=ds.Tables["who"].NewRow();
dr["name"] = "Joe";
dr["adress"] = "123 anystreet";
dr["age"] = 25;
ds.Tables["who"].Rows.Add(dr);
con.Close();
thanx in advance for all your help