gpt4 book ai didi

c# - MissingSchemaAction.AddWithKey 到底做了什么?

转载 作者:太空宇宙 更新时间:2023-11-03 23:26:32 37 4
gpt4 key购买 nike

SqlDataAdapter.MissingSchemaAction 的默认值是 MissingSchemaAction.Add,但是当我指定 AddWithKey 时,我无法理解它到底是什么做什么?

System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter();
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;

DataSet ds = new DataSet();
da.Fill(ds, "mytable");

AddWithKey 何时有用?

最佳答案

Documentation here说,它“添加必要的列和主键信息来完成模式”

它将 AddWithKey 的主要功能描述为:“这确保与现有记录匹配的传入记录被更新而不是附加。”

一点逆向工程揭示了以下内容:

当您调用 DbDataAdapter.Fill(DataSet, string) 时,它会执行 DbCommand.ExecuteReader 并将 CommandBehavior 设置为 SequentialAccess

如果您指定 MissingSchemaAction = MissingSchemaAction.AddWithKey;,则会将 CommandBehavior.KeyInfo 添加到行为中。

这会导致内部调用 DbCommand.ExecuteReader 以在您的查询之上添加以下内容:

SET NO_BROWSETABLE ON;

这是documented here微软(如下)

The browse mode lets you scan the rows in your SQL Server table and update the data in your table one row at a time. To access a SQL Server table in your application in the browse mode, you must use one of the following two options:

The SELECT statement that you use to access the data from your SQL Server table must end with the keywords FOR BROWSE. When you turn on the FOR BROWSE option to use browse mode, temporary tables are created.

You must run the following Transact-SQL statement to turn on the browse mode by using the NO_BROWSETABLE option:

SET NO_BROWSETABLE ON

When you turn on the NO_BROWSETABLE option, all the SELECT statements behave as if the FOR BROWSE option is appended to the statements. However, the NO_BROWSETABLE option does not create the temporary tables that the FOR BROWSE option generally uses to send the results to your application.

关于c# - MissingSchemaAction.AddWithKey 到底做了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33641549/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com