DataSet - 在填充外连接查询时确保 PrimaryKey 数据
填充数据集时,我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 msdn:
From msdn: