does dataadater updates 2 tables.

  • Thread starter trialproduct2004
  • Start date
T

trialproduct2004

Hi all,

i am having code as following:-

SqlCommandBuilder cmd;
da = new SqlDataAdapter("select status,id,mode from test1,test2 where
test1.id = test2.id;", mConn);
cmd = new SqlCommandBuilder(da);
DataSet ds;
ds=new DataSet();
da.Fill(ds);

da.UpdateCommand = new SqlCommand("update test1 set status = @status
where id = @id;update test2 set mode = 'p' where id = @id ",mConn );
da.UpdateCommand.Parameters.Add("@status",SqlDbType.Int,0,"status");
da.UpdateCommand.Parameters.Add("@id",SqlDbType.VarChar ,50,"id");

DataRow [] drc = ds.Tables[0].Select("id = 'O9485330060927155857741 '
");
foreach(DataRow dr in drc)
{
dr["status"] = 333;
}
da.Update(ds.Tables[0]);

Can anyone tell me whether above code is correct or not.

This code is updating 2 tables test1 and test2 using one adapter. it is
setting status in table 1 to '333' and mode in test2 to 'p'. I heard
somewhere that u can't update 2 tables with one adatper. But above
mention code is working properly in my case.

So i am not sure whetehr there is any error in my code or not. Or will
it create any error in future because i want to run this code on
database that has large amount of data.

If i am wrong please correct me.

thanks in advance.
 
P

Patrick Steele

I heard
somewhere that u can't update 2 tables with one adatper. But above
mention code is working properly in my case.

I think what you may have heard was that you can't use the
SqlCommandBuilder to automatically generate insert/update/delete
statements for you if your SELECT is coming from two tables.
 
M

Miha Markic [MVP C#]

Hi there,

Normal adapter functionallity (mosty supported from IDE) is to update a
single table but nothing prevents to use it for messing with more tables at
once.
So, your code should run just fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top