我在MS Access数据库中有一个简单的表:
-------------------
| Programs |
|-----------------|
| Id (PrimaryKey) |
| Name |
| Path |
| Notes |
-------------------
然后使用Visual Studio向导生成相应的DataTable
和TableAdapter
。
生成的Select语句看起来与预期一致:
SELECT Id, Name, Path, Notes
FROM Programs
但生成的Update语句非常复杂:
UPDATE Programs
SET Id = ?, Name = ?, Path = ?, Notes = ?
WHERE (Id = ?) AND (? = 1) AND (Name IS NULL) AND (? = 1) AND (Path IS NULL) OR
(Id = ?) AND (? = 1) AND (Name = ?) AND (Path IS NULL) OR
(Id = ?) AND (? = 1) AND (Name IS NULL) AND (Path = ?) OR
(Id = ?) AND (Name = ?) AND (Path = ?)
为什么它如此复杂? 为什么它不简单,像这样:
UPDATE Programs
SET Id = ?, Name = ?, Path = ?, Notes = ?
WHERE (Id = ?)
答案 0 :(得分:4)
因为查询检测到自上次选择以来是否对数据库进行了更改。您可以将此类检查设置为简化查询。
从“TableAdapter配置向导”中选择“高级选项...”并取消选中“使用乐观并发”
请参阅http://msdn.microsoft.com/en-us/library/aa0416cz.aspx上的乐观并发(ADO.NET)。