I know this is a silly question and I feel so stupid, but I can't find the right (the easy) way to accomplish my task.
I have an Access database imported in Visual Studio 2010 for a C# project: VS creates for me (thanks!!) strongly-typed dataset about my db. Well done.
Then, in my app, I create a new instance of this dataset CDS ds = new CDS();
and add records in its tables. Finally I do ds.AcceptChanges();
but nothing happens on db.
OK, I googled araound and think (realize?!?) I gotta open a db connection, create a DataAdapter and fill my dataset with this:
CDS ds = new CDS();
OleDbConnection conn = new OleDbConnection(path_to_db);
conn.Open();
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM mytable", conn);
da.Fill(ds.Editori); //Editori is a TableTable created automatically
// Insert rows in dataset
if (ds.HasChanges()) ds.AcceptChanges();
int ret = da.Update(ds.Editori);
Debug.Print("Update() returns: {0}", ret);
conn.Close();
but ret=0 and nothing happens on database, while in DS.Editori I have 106 rows!!
To complete my desperation: table mytable has an auto increment field as primary key; when I load ds with da, this field is correct for every record, but when I insert rows on ds, records have -1, -2, -3, etc… Why?
Can someone tells me the right way to work with strongly-typed datasets?
I gonna study, read books, I promise, but now I'm late for this job…
Thanks
UPDATE:
As suggested from Dev-Express I created the insert command, but the result is the same: when I update da, nothing happens on db.
OleDbCommand cmd = new OleDbCommand(
"INSERT INTO Editori (ID,Editore) VALUES(?,?)", conn);
cmd.Parameters.Add("@ID", OleDbType.Integer);
cmd.Parameters.Add("@Editore", OleDbType.VarChar, 255, "Editore");
da.InsertCommand = cmd;
int ret = da.Update(ds.Editori);
Debug.Print("Update() returns: {0}", ret);
conn.Close();
ANOTHER UPDATE:
I solved problem with primary keys: in generated class I manually had to change all of these lines:
this.columnID.AutoIncrementSeed = 1; // It was -1
this.columnID.AutoIncrementStep = 1; // It was -1
Best Answer
If you created a strongly typed DataSet using the Visual Studio designer, there will be a TableAdapters in the dataset or TableManager created for that DataSet.
MSDN gives the following example for usage:
Alternatively if your Visual Studio designer created a TableManager: