为什么我的合并不起作用?

发布于 2024-12-12 16:29:38 字数 884 浏览 1 评论 0原文

我确信这是典型的情况。

我有一个数据库和一个 XML 数据文档。我想将两者结合起来并更新数据库。

我已经用数据库数据“填充”了数据集和/或数据表,如果我修改该结构中的数据,我可以调用更新,一切都很好。

这是我所拥有的。

con.Open();
adapter.Fill(ds2, "BASIC");
ds.ReadXmlSchema(@"C:\asis.xsd");
ds.ReadXml("c:\\asis.xml");

// This is the XML data
DataTable loadeddt = ds.Tables["BASIC"];

// This is from the DB
DataTable dbdt = ds2.Tables["BASIC"];

// I want to put the data from xml into the dataset from the DB
dbdt.Merge(loadeddt);


// I want to them put those changes in the db
adapter.Fill(dbdt);

以下是发生的情况,在加载 XSD 之前,我收到一个异常,提示类型不匹配。

现在我什么也没得到,没有例外,没有变化。数据表和数据库都没有显示更改。

有人可以提供任何建议吗?

如果我将最后一行更改为“更新”,我会得到以下信息:

违反主键约束“PK_BASIC”。无法在对象“dbo.BASIC”中插入重复的键。

我注意到,如果您在合并前/后执行 .Rows.Count ,记录计数将从 10 变为 20,因此它将合并它们...而不是合并它们。啊!

如果我调用 dbdt.GetChanges(); 则更多它返回全部。如果我接受更改并更新它仍然没有任何作用。

I have what I am sure is a typical situation.

I have a DB and I have an XML document of data. I'd like to combine the 2 and update the db.

I have 'fill'ed a DataSet and/or DataTable with the DB data, and if I modify the data in that structure i can call Update and all is well.

Here is what I have.

con.Open();
adapter.Fill(ds2, "BASIC");
ds.ReadXmlSchema(@"C:\asis.xsd");
ds.ReadXml("c:\\asis.xml");

// This is the XML data
DataTable loadeddt = ds.Tables["BASIC"];

// This is from the DB
DataTable dbdt = ds2.Tables["BASIC"];

// I want to put the data from xml into the dataset from the DB
dbdt.Merge(loadeddt);


// I want to them put those changes in the db
adapter.Fill(dbdt);

Here is what happens, before I loaded the XSD I got an exception saying that types didn't match.

Now I get nothing, no exception no changes. neither of the DataTable show changes and neither does the DB.

Can anyone offer any suggestions?

If I change the last line to Update I get the following:

Violation of PRIMARY KEY constraint 'PK_BASIC'. Cannot insert duplicate key in object 'dbo.BASIC'.

I have noticed that if you do a .Rows.Count pre/post the merge the record count goes from 10 to 20 so it is combining them... not merging them. argh!

FUTHER MORE if I call dbdt.GetChanges(); it returns the whole lot. if i accept changes and update it still does nothing.

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

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

发布评论

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

评论(2

夜无邪 2024-12-19 16:29:39

对了,

我找到了这篇文章:

http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-ado-net/14201/Merge-function-in-DataSet

基本上XSD需要完整地描述合并工作的主键。

本质上我需要添加:

<xs:key name="ROOTKey1"  msdata:PrimaryKey="true">
    <xs:selector xpath=".//BASIC" />
    <xs:field xpath="MEMBNO" />
</xs:key>

Right guys,

I found this article:

http://www.dotnetmonster.com/Uwe/Forum.aspx/dotnet-ado-net/14201/Merge-function-in-DataSet

Basically the XSD needs to completely describe the primary key for the merge to work.

Essentially I needed to add:

<xs:key name="ROOTKey1"  msdata:PrimaryKey="true">
    <xs:selector xpath=".//BASIC" />
    <xs:field xpath="MEMBNO" />
</xs:key>
还如梦归 2024-12-19 16:29:38

您应该调用更新 而不是此行的 Fill

// I want to them put those changes in the db 
adapter.Fill(dbdt);

问题更新后:
您正在插入一个已经存在的密钥。合并告诉适配器应该在更新时插入,或者数据库填充不返回所有行。很难说清楚所给出的内容。

这看起来有点像黑客,但如果您知道哪些行应该被视为更新,您可以调用 SetModified 在这些行上。

You should call Update instead of Fill for this line:

// I want to them put those changes in the db 
adapter.Fill(dbdt);

After question update:
You're inserting a key that already exists. Either the merge is telling the adapter that it should insert when it should update or the DB fill isn't returning all rows. Hard to tell with what's given.

This seems like somewhat of a hack but if you know what rows should be considered updates, you can call SetModified on those rows.

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