- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
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/
SqlDataAdapter.MissingSchemaAction 的默认值是 MissingSchemaAction.Add,但是当我指定 AddWithKey 时,我无法理解它到底是什么做什么?
这个查询,当在 ADO.net 上运行时 MissingSchemaAction.AddWithKey抛出异常: Failed to enable constraints. One or more r
我是一名优秀的程序员,十分优秀!