开发者

Strongly-typed dataset: how to save data on db after adding data?

开发者 https://www.devze.com 2023-03-04 09:16 出处:网络
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 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.开发者_JAVA技巧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


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:

NorthwindDataSet northwindDataSet = new NorthwindDataSet();

NorthwindDataSetTableAdapters.CustomersTableAdapter customersTableAdapter = 
   new NorthwindDataSetTableAdapters.CustomersTableAdapter();
//Fill from database
customersTableAdapter.Fill(northwindDataSet.Customers);
//... changes to table
//Save changes to database
customersTableAdapter.Update(northwindDataSet.Customers);

Alternatively if your Visual Studio designer created a TableManager:

NorthwindDataSet northwindDataSet = new NorthwindDataSet();
TableAdapterManager northwindTableManager = new TableAdapterManager();

//Fill from database
northwindTableManager.CustomersTableAdapter.Fill(northwindDataSet.Customers);
//... changes to table
//Save changes to database
northwindTableManager.CustomersTableAdapter.Update(northwindDataSet.Customers);


You should also specify the UpdateCommand of the OleDBDataAdapter and then execute it by calling the da.Update method. There is an example of how this can be done in the MSDN:

OleDbDataAdapter.OleDbDataAdapter(String, OleDbConnection) Constructor

0

精彩评论

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