TableAdapter - 无需密钥更新
我是 .net c# 业务的新手,上周我热衷于创建一个表单应用程序来为 SSIS 配置打乱数据。 MS 的天才决定不对我正在使用的表应用键 - 并且生成两个候选字段的复合键将不起作用,因为它们的组合长度太长。 我并不是特别想通过添加自动增量字段来弄乱 [ssis 配置] 表架构。
因此,我在从 DataGridView 控件获取更新以使用 TableAdapter 时遇到了很多麻烦。
我需要更新语句更新表集 a = x,其中 b = y 和 c = z。
我可以设置 TableAdapter 的更新方法吗?如果可以,如何设置。 如果没有,该怎么办?
我看到这个自动生成的代码:
this._adapter.InsertCommand = new global::System.Data.SqlClient.SqlCommand();
this._adapter.InsertCommand.Connection = this.Connection;
this._adapter.InsertCommand.CommandText = "INSERT INTO [dbo].[SSIS Configurations Staging] ([ConfigurationFilter], [Configur" +
"edValue], [PackagePath], [ConfiguredValueType]) VALUES (@ConfigurationFilter, @C" +
"onfiguredValue, @PackagePath, @ConfiguredValueType)";
但在我的表单代码中,UpdateCommand 不可用。 我假设这是因为上面的代码是一个类定义,在创建对象后我无法更改它。 我看到这段代码建议不要更改,因为它是由 VS 自动生成的。
感谢您提供的最出色的建议。
I'm a total newbie at the .net c# business and dove in this last week creating a form application to shuffle data around for SSIS configurations. The geniuses at MS decided not to apply a key to the table I'm working with - and generating a composite key of the two candidate fields will not work due to their combined length being too long. I don't particularly want to mess with the [ssis configurations] table schema by adding an autoincrement field.
So I've been having alot of trouble getting an update from a DataGridView control to work with a TableAdapter.
I need the update statement to be update table set a = x where b = y and c = z.
Can I set the update method of the TableAdapter, and if so, how. If not, what to do?
I see this autogenerated code:
this._adapter.InsertCommand = new global::System.Data.SqlClient.SqlCommand();
this._adapter.InsertCommand.Connection = this.Connection;
this._adapter.InsertCommand.CommandText = "INSERT INTO [dbo].[SSIS Configurations Staging] ([ConfigurationFilter], [Configur" +
"edValue], [PackagePath], [ConfiguredValueType]) VALUES (@ConfigurationFilter, @C" +
"onfiguredValue, @PackagePath, @ConfiguredValueType)";
But in my form code, the UpdateCommand is not available. I'm assuming this is because the above code is a class definition which I cannot change after creating the object. I see this code has a recommendation not to be changed since it is autogenerated by VS.
Thanks for your most excellent advice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从您的代码中,我假设您正在与设计器一起使用类型化数据集。
没有主键是设计者不会生成插入、更新或删除命令的众多原因之一。 这是 CommandBuilder 的限制。
您可以使用属性窗口向 Apdapter 添加更新命令,但我建议您不要这样做,如果您稍后再次配置主查询,它会很高兴地丢弃您的所有工作。 同样的论点也反对修改任何 *.Designer.cs 文件中的代码。
相反,双击带有适配器名称的标题。 设计者将创建(如果需要)附带的非设计者源文件,并将分部类的外部放入其中。 不幸的是,这就是设计器在 C# 中的代码生成的程度,您必须从那里开始。 (旁白:VB 设计者还知道一些技巧)。
编辑:
您必须提供自己的 Update(...) 方法并设置 UpdateCommand 等。
From your code i assume you are using a typed Dataset with the designer.
Not having a primary key is one of the many reasons the designer will not generate Insert, Update or Delete commands. This is a limitation of the CommandBuilder.
You could use the properties window to add an Update Command to the Apdapter but I would advice against that, if you later configure your main query again it will happily throw away all your work. The same argument holds against modifying the code in any *.Designer.cs file.
Instead, doubleclick on the caption with the Adaptername. The designer will create (if necessary) the accompanying non-designer source file and put the outside of a partial class in it. Unfortunately that is how far the code-generation of the designer goes in C#, you'll have to take it from there. (Aside: The VB designer knows a few more tricks).
Edit:
You will have to provide your own Update(...) method and setup an UpdateCommand etc.