JOINed表上的TableAdapter UpdateCommand [英] TableAdapter UpdateCommand on JOINed table
问题描述
假设我有一个从TableAdapter加载的DataGridView,其内容来自2个JOINed表,因此表C为:
Suppose I have a DataGridView that is loading from a TableAdapter whose content is from 2 JOINed tables, so Table C is:
SELECT A.*, B.name LEFT JOIN B ON B.id = A.b_id
$我知道,b $ b
向导没有为此生成UpdateCommand。但是,如果表中的数据几乎全部来自表A,并且仅将表B联接起来以提供表A中id所引用的数据的名称,那么我是否可以提供自己的UpdateCommand来更新用户(如果用户更改了表A)
No UpdateCommand is generated for this by the wizard, I know. However, if the data of the table is almost entirely from Table A, and Table B is only joined in to provide the name of data referenced by id in Table A, can I just supply my own UpdateCommand that updates Table A if the user changes a value in the DataGridView?
就是说,我想将表C的UpdateCommand设置为:
That is, I would like to set Table C's UpdateCommand to:
UPDATE A SET value = [[new value]] WHERE id = [[current item]]
如果情况变得更糟,我可以为用户提供一个对话框,以输入新值并以这种方式进行操作。似乎像上面那样做要简单得多。
If worse comes to worse, I can make a dialog for the user to enter their new value into and do it that way. It just seems like it would be a lot simpler to do it as above. Will that method work?
推荐答案
您可以在 DataAdapter
。您可以找到在MSDN上很好的演练
You can do exactly what you want within the confines of the DataAdapter
. You can find a good walkthrough on MSDN.
没有看到您的代码,适配器的设置可能看起来像这样:
Without seeing your code, the setup of your adapter might look something like this:
var dataAdapter = new SqlDataAdapter(
"SELECT A.*, B.name FROM A LEFT JOIN B ON B.id = A.b_id", sqlConn);
var dataAdapter.UpdateCommand = new SqlCommand(
"UPDATE A SET value = @Value WHERE id = @Id", sqlConn);
// Define the parameters to be used in the update command.
dataAdapter.UpdateCommand.Parameters.Add(
"@Value", SqlDbType.NVarChar, 100, "Value_Column_Name");
dataAdapter.UpdateCommand.Parameters.Add(
new SqlParameter("@Id", SqlDbType.Int)
{
SourceColumn = "Id_Column_Name"
});
这篇关于JOINed表上的TableAdapter UpdateCommand的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!