MissingSchemaAction.AddWithKey真正做什么? [英] What does MissingSchemaAction.AddWithKey really do?

查看:573
本文介绍了MissingSchemaAction.AddWithKey真正做什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SqlDataAdapter.MissingSchemaAction 的默认值为 MissingSchemaAction.Add ,但是当我指定<$ c $时c> AddWithKey 我不明白它真正在做什么?

The default value of SqlDataAdapter.MissingSchemaAction is MissingSchemaAction.Add, but when I specify the AddWithKey I can't understand what it really do ?

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 says, it "adds the necessary columns and primary key information to complete the schema"

它声明 AddWithKey 的主要功能为:这确保匹配现有记录的传入记录被更新而不是附加。

It states the primary function of AddWithKey as: "This ensures that incoming records that match existing records are updated instead of appended."

一些反向工程揭示了以下内容:

A little reverse engineering reveals the following:

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

如果指定 MissingSchemaAction = MissingSchemaAction.AddWithKey; CommandBehavior.KeyInfo 已添加到该行为。

If you specify MissingSchemaAction = MissingSchemaAction.AddWithKey; the CommandBehavior.KeyInfo is added to the behavior.

这将导致内部调用 DbCommand.ExecuteReader 在查询上方添加以下内容:

This causes the DbCommand.ExecuteReader invoked internally to add the following on top of your query:

SET NO_BROWSETABLE ON;

在此处记录的信息(如下所示)


浏览模式使您可以扫描SQL Server表中的行,并且
一次更新表中的数据。要在浏览模式下访问应用程序中的SQL
服务器表,必须使用以下两个选项中的一个

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:

SELECT语句用于从SQL
服务器表访问数据的表必须以关键字FOR BROWSE结尾。当打开
的FOR BROWSE选项以使用浏览模式时,将创建
临时表。

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.

您必须运行以下Transact-SQL语句使用NO_BROWSETABLE选项打开
浏览模式:

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

SET NO_BROWSETABLE ON

当您打开NO_BROWSETABLE选项时,所有SELECT语句
的行为就像FOR BROWSE选项被附加到了语句之后。
但是,NO_BROWSETABLE选项不会创建FOR BROWSE选项通常用于将结果
发送到您的应用程序的临时
表。

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.

这篇关于MissingSchemaAction.AddWithKey真正做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆