求助.net事务处理!!!!谢谢
本人想处理事务删除两个相关表中的内容,但以下代码会自动产生异常直接抛出,无法正常运行。希望各位高手可以帮忙,谢谢(存储过程没有问题,在没加事务之前正常删除过表中内容)
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;
namespace Group_ComputerInformation
{
public class Student_Computer
{
private static string connectiondatabase = @"Server= 192.168.151.174;User=sa;PWD=000000 ;Database = StudentManagement;";
/// <summary>
/// 删除表Student_OperationSystem中的信息,需先删除与之相关表中内容,此过程为删除相关表的内容
/// </summary>
/// <param name="operationsystem_ID">删除条件参数</param>
public void OS_delete_ComputerInformation(SqlConnection database,int operationsystem_ID)
{
SqlCommand cmd = new SqlCommand("OS_delete_ComputerInformation",database);
= CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add("@OperationSystem_ID", SqlDbType.Int);
cmd.Parameters["@OperationSystem_ID"].Value = operationsystem_ID;
cmd.ExecuteNonQuery();
}
/// <summary>
/// 删除表Student_OperationSystem中的信息,删除过相关表后,才执行次过程
/// </summary>
/// <param name="operationsystem_ID">删除条件参数</param>
public void OS_delete_OperationSystem(SqlConnection database,int operationsystem_ID)
{
SqlCommand cmd = new SqlCommand("OS_delete_OperationSystem",database);
= CommandType.StoredProcedure;
cmd.Parameters.Clear();
cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.Parameters["@ID"].Value = operationsystem_ID;
cmd.ExecuteNonQuery();
}
/// <summary>
/// 删除表Student_OperationSystem中内容的事务处理
/// </summary>
/// <param name="operationsystem_ID"></param>
public void TS_OS_delete_OperationSystem(int operationsystem_ID)
{
SqlConnection database = new SqlConnection(connectiondatabase);
database.Open();
SqlTransaction sqlTran = database.BeginTransaction();
SqlCommand command = database.CreateCommand();
command.Connection = database;
command.Transaction = sqlTran;
try
{
OS_delete_ComputerInformation(database,operationsystem_ID);
OS_delete_OperationSystem(database,operationsystem_ID);
();
}
catch (Exception ex)
{
sqlTran.Rollback();
}
finally
{
database.Close();
}
}
}