强类型数据集:添加数据后如何将数据保存到db上?

发布于 2024-11-05 14:26:21 字数 1628 浏览 0 评论 0原文

我知道这是一个愚蠢的问题,我觉得自己很愚蠢,但我找不到正确(简单)的方法来完成我的任务。
我在 Visual Studio 2010 中为 C# 项目导入了一个 Access 数据库:VS 为我创建了(谢谢!!)关于我的数据库的强类型数据集。干得好。
然后,在我的应用中,我创建此数据集的新实例 CDS ds = new CDS(); 并在其表中添加记录。最后我做了 ds.AcceptChanges(); 但数据库上没有任何反应。
好的,我用 google 搜索了 araound 并认为(意识到?!?)我必须打开一个数据库连接,创建一个 DataAdapter 并用以下内容填充我的数据集:

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();

但是 ret=0 并且数据库上没有任何反应,而在 DS.Editori 中我有 106 行! !

为了完成我的绝望:表mytable有一个自动增量字段作为主键;当我用 da 加载 ds 时,该字段对于每条记录都是正确的,但是当我在 ds 上插入行时,记录有 -1、-2、-3 等......为什么?

有人可以告诉我使用强类型数据集的正确方法吗? 我会学习、读书,我保证,但现在我迟到了…… 谢谢

更新:
根据 Dev-Express 的建议,我创建了插入命令,但结果是相同的:当我更新 da 时,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();

另一个更新: 我解决了主键的问题:在生成的类中,我必须手动更改所有这些行:

this.columnID.AutoIncrementSeed = 1; // It was -1
this.columnID.AutoIncrementStep = 1; // It was -1

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

旧时浪漫 2024-11-12 14:26:21

如果您使用 Visual Studio 设计器创建强类型数据集,则会有一个 TableAdapters< /a> 在数据集或为该数据集创建的 TableManager 中。

MSDN 给出了以下使用示例:

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);

或者,如果您的 Visual Studio 设计器创建了一个 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);

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);
挖鼻大婶 2024-11-12 14:26:21

您还应该指定 OleDBDataAdapter 的 UpdateCommand,然后通过调用 da.Update 方法来执行它。 MSDN 中有一个如何完成此操作的示例:

OleDbDataAdapter.OleDbDataAdapter(String, OleDbConnection) 构造函数

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文