Click here to Skip to main content
15,892,965 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to excute insert and update sql quries on one button event in C#...

i want to to this... so that i will update on table in datable and insertsome data in another table. at the same time...


can anyone give me some idea about this..
Posted

Use a procedure with both insert and update queries.

Check the TestProc

SQL
CREATE PROCEDURE TestProc
@Column1 varchar(50),
@Column2 varchar(50),
@Column3 varchar(50),
@Column4 varchar(50)
	
AS
BEGIN
	
insert into Table1(Column1, Column2) values (@Column1, @Column2)

update Table2 set Column4 = @Column4 where column3=@column3

END
 
Share this answer
 
Hi,

I believe the above answers are good enough. But just so that you know, you can use transactions to execute multiple inserts/updates/deletes and rollback all changes if any of them fails.

Just take a look at this Link[^]

Hope this helps, Regards.
 
Share this answer
 
v2
this[^] might help you.
 
Share this answer
 
Use Multiple Active Result Sets (MARS) which allows you to execute multiple queries or stored procedures on a single connection
take a look there-[Want to Execute Multiple Queries on a Single Connection][^].
this article demonstrates how to utilize MARS and also discusses the situations in which MARS is appropriate and can provide performance benefits
 
Share this answer
 
Hi,

Create transactions, and insert and commit or rollback the insertion based on the output and then perform the update accordingly and again update the transactions.

some thing like the followings.

connection.transactions
int i = insertIntoMyTable();// call the insertion operation

if(i>0)
{
//Commit transaction
cn. transaction
int u = UpdateIntoMyTable();// call the update method
if(u>0)
{
//commit transaction
}
else
{
//rollback transaction
}
}
else
{
// Rollback the transaction
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900