c#事务处理

来源:互联网 发布:单片机测温电路 编辑:程序博客网 时间:2024/05/01 00:40
public void TransactionTest()
        
{
            
string connectionString = "";
            SqlConnectionconnection 
= new SqlConnection(connectionString);
            connection.Open();
            SqlCommandcommand 
= new SqlCommand();
            command.Connection 
= connection;
            SqlTransaction transaction;
            transaction 
= connection.BeginTransaction(); 
            command.Transaction 
= transaction;
            
try
            
{
                
//代码
        myCommand.CommandText = "SQL语句";
        myCommand.ExecuteNonQuery();
                transaction.Commit();
            }

            
catch
            
{
                transaction.Rollback(); 
            }

            
finally
            
{
                connection.Close();
            }

        }
 
原创粉丝点击