你们是怎莫在web程序里面处理:A表的字段b是B表的主键 b是A的外键的数据的?在线等....
现在有3个表,A、B、C
A: B: C:
aid title concent bid aid name cid aid name
aid是表A的主键 bid是表B的主键 cid是表C的主键
aid是表B的主键 aid是C表的主键
现在要向三个表中插入数据或删除数据
大家一般是怎莫处理的 请执教一下 最好给个小例子
使用事务的话该怎莫做呢??
解决马上结帖
------解决方案--------------------晕``
插入先主后子
删除先子后主
------解决方案--------------------Begin TRANSACTION
插入时先A后BC
删除时先BC后A
if(@@error <> 0)
COMMIT TRANSACTION
else
ROLLBACK TRANSACTION
------解决方案--------------------CREATE PROCEDURE IR_rb_User_UpdateUser
(
@UserID int,
@Name nvarchar(50),
@Email nvarchar(100),
@Password nvarchar(20),
@RoleID int,
@iSuccess int output
)
AS
set @iSuccess = -1 --判断是否成功有错误应为插入重复键
IF EXISTS (SELECT Email FROM rb_Users WHERE Email = @Email and UserID <> @UserID)
BEGIN
set @iSuccess = -2
return
END
BEGIN TRAN
UPDATE rb_Users SET Name = @Name, Email = @Email, Password = @Password WHERE UserID = @UserID
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
return
END
set @iSuccess = -3
update rb_UserRoles set RoleID=@RoleID where UserID=@UserID
IF @@ERROR <> 0
BEGIN
ROLLBACK TRAN
return
END
COMMIT TRAN
set @iSuccess = 1
GO
给你看别人写的
------解决方案--------------------如果用的是SqlServer数据库,可以看看SqlTransaction类...
可以看看MSDN:
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.NETDEVFX.v20.chs/cpref4/html/T_System_Data_SqlClient_SqlTransaction.htm
给你一个上面的例子:
private static void ExecuteSqlTransaction(string connectionString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
// Start a local transaction.
transaction = connection.BeginTransaction( "SampleTransaction ");
// Must assign both transaction object and connection
// to Command object for a pending local transaction
command.Connection = connection;
command.Transaction = transaction;
try
{
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description ') ";
command.ExecuteNonQuery();
command.CommandText =
"Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description ') ";
command.ExecuteNonQuery();