1. Sql写法(事物+游标)
--当两个或两以上的操作要么都执行,要么都执行时要用事务。
--开始事务
BEGIN TRAN
--不显示计数信息
SET NOCOUNT ON
DECLARE @ProjNo varchar(50),@CusNo varchar(50)
--声明游标
DECLARE CRMPSContact_cursor CURSOR FOR
SELECT ProjNo
FROM CRMPSContact
WHERE ProjNo>0
--打开游标
OPEN CRMPSContact_cursor
--取第一行的值给专案变量: @ProjNo
FETCH NEXT FROM CRMPSContact_cursor
INTO @ProjNo
--取得客户号
select @CusNo = CusNo
from CRMPSProjectM
where ProjNo = @ProjNo
--得到CRMPSContact中某专案号对应的客户号
update CRMPSContact
set CusNo = @CusNo
where ProjNo = @ProjNo
--执行错误回滚
if @@error!=0
begin
rollback tran
return
end
--移动游标,其它所有行更新操作(当到结尾时退出)
WHILE @@FETCH_STATUS = 0
BEGIN
--游标移到下一行
FETCH NEXT FROM CRMPSContact_cursor
INTO @ProjNo
--取得客户号
select @CusNo = CusNo
from CRMPSProjectM
where ProjNo = @ProjNo
--得到CRMPSContact中某专案号对应的客户号
update CRMPSContact
set CusNo = @CusNo
where ProjNo = @ProjNo
--执行错误回滚
if @@error!=0
begin
rollback tran
return
end
END
--提交所有变更
COMMIT TRAN
--关闭游标
CLOSE CRMPSContact_cursor
--释放游标
DEALLOCATE CRMPSContact_cursor
--恢复设置
SET NOCOUNT OFF
GO
--开始事务
BEGIN TRAN
--不显示计数信息
SET NOCOUNT ON
DECLARE @ProjNo varchar(50),@CusNo varchar(50)
--声明游标
DECLARE CRMPSContact_cursor CURSOR FOR
SELECT ProjNo
FROM CRMPSContact
WHERE ProjNo>0
--打开游标
OPEN CRMPSContact_cursor
--取第一行的值给专案变量: @ProjNo
FETCH NEXT FROM CRMPSContact_cursor
INTO @ProjNo
--取得客户号
select @CusNo = CusNo
from CRMPSProjectM
where ProjNo = @ProjNo
--得到CRMPSContact中某专案号对应的客户号
update CRMPSContact
set CusNo = @CusNo
where ProjNo = @ProjNo
--执行错误回滚
if @@error!=0
begin
rollback tran
return
end
--移动游标,其它所有行更新操作(当到结尾时退出)
WHILE @@FETCH_STATUS = 0
BEGIN
--游标移到下一行
FETCH NEXT FROM CRMPSContact_cursor
INTO @ProjNo
--取得客户号
select @CusNo = CusNo
from CRMPSProjectM
where ProjNo = @ProjNo
--得到CRMPSContact中某专案号对应的客户号
update CRMPSContact
set CusNo = @CusNo
where ProjNo = @ProjNo
--执行错误回滚
if @@error!=0
begin
rollback tran
return
end
END
--提交所有变更
COMMIT TRAN
--关闭游标
CLOSE CRMPSContact_cursor
--释放游标
DEALLOCATE CRMPSContact_cursor
--恢复设置
SET NOCOUNT OFF
GO
C#中利用SqlAdapter对DS执行Update命令
public void UpdateContactTableByDataSet(DataSet ds, string strTblName)
{
try
{
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand myCommand = new SqlCommand(SQL_GetAllRecordFromDemand(strTblName), conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
conn.Open();
SqlTransaction myTrans = conn.BeginTransaction();
myCommand.Transaction = myTrans;
try
{
myAdapter.Update(ds, strTblName);
myTrans.Commit();
}
catch (Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("回滚失败! 异常类型: " + ex.GetType());
}
}
}
finally
{
conn.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}
{
try
{
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlConnection conn = new SqlConnection(strConnection);
SqlCommand myCommand = new SqlCommand(SQL_GetAllRecordFromDemand(strTblName), conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
conn.Open();
SqlTransaction myTrans = conn.BeginTransaction();
myCommand.Transaction = myTrans;
try
{
myAdapter.Update(ds, strTblName);
myTrans.Commit();
}
catch (Exception e)
{
try
{
myTrans.Rollback();
}
catch (SqlException ex)
{
if (myTrans.Connection != null)
{
Console.WriteLine("回滚失败! 异常类型: " + ex.GetType());
}
}
}
finally
{
conn.Close();
}
}
catch (Exception ex)
{
throw ex;
}
}