DataSet - 在填充外连接查询时确保 PrimaryKey 数据

发布于 2024-09-04 22:02:44 字数 899 浏览 7 评论 0原文

填充数据集时,我使用 DataAdapter 的 MissingSchemaAction 的“AddWithKey”值。根据 MSDN:

将架构信息添加到数据集 在填充数据之前确保 主键约束是 包含在 DataTable 对象中 数据集。

但是,当我执行以下操作时:

DataColumn[] foo = TheDataSet.Tables["Table1"].PrimaryKey;

我得到 foo.Length 为 0。除此之外我还需要做些什么吗?:

string TheQuery = "SELECT * FROM Table1 LEFT OUTER JOIN Table2 ON Table1.EVENT_NUM = Table2.EVENT_USER_NUM;
using (SqlDataAdapter TheDataAdapter = new SqlDataAdapter(TheQuery, TheConnection)
{
    TheDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    TheDataAdapter.Fill(TheDataSet, "Table1");
}

注意,如果我的查询是:

string TheQuery = "SELECT * FROM Table1";

一切都会好的。但是,如果有连接,我不会得到主键。这是否可能(获取主键)?

编辑:起初,我没有意识到这是一个 JOIN。不过,我已经测试了会返回 PrimaryKey 的 INNER JOINS。罪魁祸首似乎是因为这是一个 OUTER JOIN。因此,我改变了标题。

When filling a DataSet, I am using the "AddWithKey" value for the DataAdapter's MissingSchemaAction. According to MSDN:

Adding schema information to a DataSet
before filling it with data ensures
that primary key constraints are
included with the DataTable objects in
the DataSet.

But, when I do the following:

DataColumn[] foo = TheDataSet.Tables["Table1"].PrimaryKey;

I get foo.Length of 0. Is there something more that I need to do besides this?:

string TheQuery = "SELECT * FROM Table1 LEFT OUTER JOIN Table2 ON Table1.EVENT_NUM = Table2.EVENT_USER_NUM;
using (SqlDataAdapter TheDataAdapter = new SqlDataAdapter(TheQuery, TheConnection)
{
    TheDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
    TheDataAdapter.Fill(TheDataSet, "Table1");
}

Note, if my query was:

string TheQuery = "SELECT * FROM Table1";

All would be fine. But, I don't get a PrimaryKey if there is a join. Is this even possible (to get the Primary key)?

EDIT: At first, I didn't realize this was a JOIN. Although, I've tested INNER JOINS that would return a PrimaryKey. The culprit seems to be the fact that this is an OUTER JOIN. Thus, I changed the title.

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

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

发布评论

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

评论(1

枫以 2024-09-11 22:02:44

来自 msdn

如果 SelectCommand 返回
OUTER JOIN 的结果,
DataAdapter不会设置PrimaryKey
结果数据表的值。你
必须自己定义PrimaryKey
确保重复的行
正确解决。

From msdn:

If the SelectCommand returns the
results of an OUTER JOIN, the
DataAdapter will not set a PrimaryKey
value for the resulting DataTable. You
must define the PrimaryKey yourself to
ensure that duplicate rows are
resolved correctly.

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