C#,SQL:在一次调用中收集数据表行和主键信息?
我想使用从SQL表收集数据以及该表的C#,SQL:在一次调用中收集数据表行和主键信息?,c#,sql,database,C#,Sql,Database,我想使用从SQL表收集数据以及该表的PrimaryKey信息 我只知道如何做其中一个。如果我指定收集KeyInfo,则不会收集数据。如果使用默认值并收集数据,则不会收集KeyInfo DataTable Collect(string tableName) { DataTable table = new DataTable(tableName); if (!String.IsNullOrEmpty(tableName)) { using (SqlCeCommand cmd = ne
PrimaryKey
信息
我只知道如何做其中一个。如果我指定收集KeyInfo,则不会收集数据。如果使用默认值并收集数据,则不会收集KeyInfo
DataTable Collect(string tableName) {
DataTable table = new DataTable(tableName);
if (!String.IsNullOrEmpty(tableName)) {
using (SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM " + tableName, new SqlCeConnection(Connection))) {
cmd.Connection.Open();
// this line will collect the Primary Key information: (Rows.Count = 0)
table.Load(cmd.ExecuteReader(CommandBehavior.KeyInfo));
// this line will collect the Row Data:
table.Load(cmd.ExecuteReader());
// I know the using clause is supposed to do this, but it can take too long
cmd.Connection.Close();
}
}
return table;
}
我已尝试使用两个标志收集这两个标志:
CommandBehavior cb = CommandBehavior.Default | CommandBehavior.KeyInfo
table.Load(cmd.ExecuteReader(cb));
但是,这仍然会导致返回0个数据行
当然,我可以进行两(2)次调用并合并数据,但这是必要的吗?没有更干净的吗?CommandBehavior的整数值。默认值为零。这就是为什么这个
CommandBehavior.Default | CommandBehavior.KeyInfo
与仅仅CommandBehavior.KeyInfo
但是,调用CommandBehavior.KeyInfo
只会将信息添加到架构中,而不会像对OleDb和SQLClient那样影响返回的行
我猜这是特定于SqlCe对象的 好的,我明白了。那么,没有办法将
PrimaryKey
info与行数据一起返回?@jp2code我仔细查看了CommandBehavior枚举及其行为。我已经更新了我的答案。我认为这与CE客户端有关(但可能是服务器)