我用這個命令在多行插入多條記錄,如果插入不成功,我怎樣才能鎖定我的命令和回滾更改?如何鎖定插入多個表中?
SqlCommand cmd = new SqlCommand();
string s = @"
declare @one_id int;
INSERT INTO tbl_one(o1,o2,o3) VALUES(@o1,@o2,@o3);
set @one_id=SCOPE_IDENTITY();
INSERT INTO tbl_two(t1,t2,f3) VALUES(@t1,@t2,@one_id);
INSERT INTO tbl_two(t1,t2,f3) VALUES(@t3,@t4,@one_id);
INSERT INTO tbl_two(t1,t2,f3) VALUES(@t5,@t6,@one_id);
";
cmd.CommandText =s;
使用交易:http://www.codeproject.com/Articles/10223/Using-Transactions-in-ADO-NET – 2015-02-09 08:45:13