Chinaunix首页 | 论坛 | 博客
  • 博客访问: 14987578
  • 博文数量: 7460
  • 博客积分: 10434
  • 博客等级: 上将
  • 技术积分: 78178
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-02 22:54
文章分类

全部博文(7460)

文章存档

2011年(1)

2009年(669)

2008年(6790)

分类: 数据库开发技术

2008-05-28 11:25:42

 private static readonly string strCon = "server=.;database=test;uid=sa;pwd=123";
    SqlConnection con 
= new SqlConnection(strCon);
    
public string aaa()
    
{
        con.Open();
        SqlTransaction sta 
= con.BeginTransaction(IsolationLevel.ReadCommitted, "aaa");
        SqlCommand cmd 
= con.CreateCommand();
        cmd.Connection 
= con;
        cmd.Transaction 
= sta;

        
try
        
{
            
            cmd.CommandText 
= "insert into t1 (tAction) values ('a3')";
            cmd.ExecuteNonQuery();
            cmd.CommandText 
= "insert into t1 (tAction) values ('a4')";
            cmd.ExecuteNonQuery();
            sta.Commit();
            
return "success";
        }

        
catch (Exception e)
        
{
            
try
            
{
                sta.Rollback();
            }

            
catch (SqlException ex)
            
{
                
if (sta.Connection != null)
                
{
                    
return "An exception of type " + ex.GetType().ToString();
                }

            }

            
return e.GetType().ToString();
        }

        
finally
        
{
            con.Close();
        }

    }
阅读(346) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~