首页 > 解决方案 > Datagridview update to multiple tables without using bindingsource or tableadapter

问题描述

I have a working Select query but since it contains multiple tables I can't update it from my datagridview.
I'm sure it can be re-written to allow updates, but my SQL is quite limited.

SELECT 
d.Unique_Part_ID, 
d.Location_ID, 
d.Recorded_Value, 
d.Date_Time_Stamp, 
d.Traceability, 
d.Part_Status, 
d.Measure_State,
p.Part_Number,  
p.Part_Rev,
c.CN, 
c.Characteristic_Requirement, 
c.Characteristic_Name, 
c.Tol_min, 
c.Nominal_Value, 
c.Tol_Max, 
c.Proprietary_Tag 
FROM GE_KC_Data AS d 
INNER JOIN GE_Key_Characteristics AS c ON d.Characteristic_ID = c.ID 
INNER JOIN GE_Parts AS p ON c.PartID = p.ID 
WHERE IsNull(d.Reported,'False')='False' 
ORDER BY d.Date_Time_Stamp DESC

Any of you genius's able to help? Using subquery statements also returns error "Only one expression can be specified in the select list when the subquery is not introduced with EXISTS" And of course I have no idea what that means.

标签: sqlvb.netsql-server-2008datagridview

解决方案


我确定它可以重写以允许更新

它不能。决定您将更新哪个表并为其编写更新语句并将其分配给您正在使用的数据适配器的 updatecommand 属性

如果您要更新两个不同的表,请编写一个存储过程来进行更新,将数据传递给存储过程(同样,将其分配给数据适配器的更新命令)并更新存储过程中的多个表

或者循环datagridview绑定的数据表,自己手动提交更新的行,使用多个更新查询

对于在这种情况下我有一个主/从数据关系来表示在表单上的情况,我不会尝试在一个网格中表示它们,我使用两个,对每个表都有一个简单的选择,从而有一个数据集使用可更新表,我在两个表之间建立数据关系,然后使从网格的绑定源具有(主绑定源)的数据源和(数据集的数据关系的名称)的数据属性名称。这意味着当在主网格中选择主行时,从网格过滤到仅与所选主网格相关的行

因此,您最终不会在单个网格中重复主单元格(如果您有订单和订单项,那么订单数据会为每个订单项重复:

  ordernum | customer name | item ordered
  123      | John Smith    | washer pump
  123      | John Smith    | door seal

如果将一行的名称编辑为 Jane Smith,将另一行的名称编辑为 John Jones,哪一行获胜?

拥有两个网格和链接数据是执行此操作的正确方法。有关更多信息,请参阅:

https://msdn.microsoft.com/en-us/library/fxsa23t6.aspx

建议您从阅读“创建简单数据应用程序”教程开始,无论其他人对您的情况有多适用;)


推荐阅读