欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#中事务处理和非事务处理方法实例分析

程序员文章站 2023-11-20 21:39:58
本文实例讲述了c#中事务处理和非事务处理方法。分享给大家供大家参考。具体如下: c#代码如下: string connectionstring = config...

本文实例讲述了c#中事务处理和非事务处理方法。分享给大家供大家参考。具体如下:

c#代码如下:

string connectionstring = configurationmanager.connectionstrings["db_constr"].tostring();
sqlconnection conn = null;
sqltransaction trans = null;
try {
 conn = new sqlconnection(connectionstring);
 if (conn.state != connectionstate.open) conn.open();
 //开启事务,可以指定事无级别。默认为提交才可以查询
 trans = conn.begintransaction();
 //save(conn, trans);
 dbutils.commit(trans);
} catch (exception ex) {
 dbutils.rollback(trans);
 clientscript.registerstartupscript(this.gettype(), "错误提示", "<script>alert('" + stringutil.tojsstr(ex.message) + "');</script>");
} finally {
 dbutils.closequietly(conn, trans);
}
string connectionstring = configurationmanager.connectionstrings["db_constr"].tostring();
sqlconnection conn = null;
try {
 conn = new sqlconnection(connectionstring);
 if (conn.state != connectionstate.open) conn.open();
} catch (exception ex) {
 scriptmanager.registerstartupscript(this, this.gettype(), "mess", "<script>alert('"+stringutil.tojsstr(ex.message)+"')</script>", false);
} finally {
 dbutils.closequietly(conn);
}

stringutil.cs如下:

using system;
using system.collections.generic;
using system.text;
namespace tools{
 public sealed class stringutil {
  public static bool isempty(string str) {
   if (str == null) return true;
   if (str.length == 0) return true;
   if (str == string.empty) return true;
   return false;
  }
  public static bool isnotempty(string str) {
   return !isempty(str);
  }
  public static string tojsstr(string str) {
   str = str.replace("'", "\\'");
   str = str.replace("\r", "");
   str = str.replace("\n", "\\n");
   return str;
  }
  public static void main() {
   console.write(isnotempty(null));
   console.read();
  }
 }
}

dbutils.cs如下:

using system;
using system.collections.generic;
using system.text;
using system.data.sqlclient;
using system.data;
namespace tools{
 public class dbutils {
  public static sqlconnection getconnection(string connectstr) {
   sqlconnection conn = new sqlconnection(connectstr);
   if (conn.state != connectionstate.open) {
    conn.open();
   }
   return conn;
  }
  public static void closequietly(sqlconnection conn) {
   try {
    close(conn);
   } catch { }
  }
  public static void closequietly(sqltransaction trans) {
   try {
    close(trans);
   } catch { }
  }
  public static void close(sqlconnection conn) {
   if (conn != null) {
    conn.close();
    conn.dispose();
   }
  }
  public static void close(sqltransaction trans) {
   if (trans != null) {
    trans.dispose();
   }
  }
  public static void closequietly(sqlconnection conn, sqltransaction trans) {
   closequietly(trans);
   closequietly(conn);
  }
  public static void commit(sqltransaction trans) {
   try {
    if(trans!=null) trans.commit();
   } catch{ }
  }
  public static void rollback(sqltransaction trans) {
   try {
    if (trans != null) trans.rollback();
   } catch { }
  }
 }
}

希望本文所述对大家的c#程序设计有所帮助。