SoFunction
Updated on 2025-03-07

Example analysis of transaction and non-transaction processing methods in C#

This article describes transactional and non-transactional methods in C#. Share it for your reference. The details are as follows:

The C# code is as follows:

String connectionString = ["DB_Constr"].ToString();
SqlConnection conn = null;
SqlTransaction trans = null;
try {
 conn = new SqlConnection(connectionString);
 if ( != ) ();
 //Open the transaction and specify that there is no level of the matter.  The default is to submit it to query trans = ();
 //save(conn, trans);
 (trans);
} catch (Exception ex) {
 (trans);
 ((), "Error prompt", "<script>alert('" + () + "');</script>");
} finally {
 (conn, trans);
}
String connectionString = ["DB_Constr"].ToString();
SqlConnection conn = null;
try {
 conn = new SqlConnection(connectionString);
 if ( != ) ();
} catch (Exception ex) {
 (this, (), "mess", "<script>alert('"+()+"')</script>", false);
} finally {
 (conn);
}

as follows:

using System;
using ;
using ;
namespace Tools{
 public sealed class StringUtil {
  public static bool isEmpty(String str) {
   if (str == null) return true;
   if ( == 0) return true;
   if (str == ) return true;
   return false;
  }
  public static bool isNotEmpty(String str) {
   return !isEmpty(str);
  }
  public static String toJsStr(String str) {
   str = ("'", "\\'");
   str = ("\r", "");
   str = ("\n", "\\n");
   return str;
  }
  public static void Main() {
   (isNotEmpty(null));
   ();
  }
 }
}

as follows:

using System;
using ;
using ;
using ;
using ;
namespace Tools{
 public class DbUtils {
  public static SqlConnection getConnection(string connectstr) {
   SqlConnection conn = new SqlConnection(connectstr);
   if ( != ) {
    ();
   }
   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) {
    ();
    ();
   }
  }
  public static void close(SqlTransaction trans) {
   if (trans != null) {
    ();
   }
  }
  public static void closeQuietly(SqlConnection conn, SqlTransaction trans) {
   closeQuietly(trans);
   closeQuietly(conn);
  }
  public static void commit(SqlTransaction trans) {
   try {
    if(trans!=null) ();
   } catch{ }
  }
  public static void rollback(SqlTransaction trans) {
   try {
    if (trans != null) ();
   } catch { }
  }
 }
}

I hope this article will be helpful to everyone's C# programming.