SqlConnection cn = new SqlConnection("Data Source = (local);Integrated Security = SSPI;Initial Catalog=northwind");cn.Open();SqlTransaction ta = cn.BeginTransaction();
SqlCommand cmd = cn.CreateCommand();
cmd.Transaction = ta;try
{
    //你的数据操作
    ta.Commit();
    Console.WriteLine("成功");
}
catch (Exception e)
{
    try
    {
        ta.Rollback();
    }
    catch (SqlException ex)
    {
        if (ta.Connection != null)
        {
            Console.WriteLine(ex.GetType() + " 回滚事务");
        }
    }
    Console.WriteLine(e.GetType());
    Console.WriteLine("没有数据写入数据库");
}
finally
{
    cn.Close();
}