C#中事务处理和非事务处理方法实例分析
作者:罪恶的花生 时间:2023-12-23 08:09:13
本文实例讲述了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#程序设计有所帮助。
标签:C#,事务处理
0
投稿
猜你喜欢
idea生成类注释和方法注释的正确方法(推荐)
2022-09-11 03:45:14
Java lambda表达式与泛型整理总结
2021-07-15 19:57:14
基于java中cookie和session的比较
2021-08-17 00:49:44
java加密算法分享(rsa解密、对称加密、md5加密)
2021-08-30 16:22:08
Android开发之ListView列表刷新和加载更多实现方法
2021-06-20 06:28:30
Java 实现二叉搜索树的查找、插入、删除、遍历
2023-10-04 05:37:39
IntelliJ IDEA中查看当前类的所有继承关系图
2023-08-06 12:40:19
Android 动态菜单实现实例代码
2023-05-19 16:24:58
IDEA插件开发之环境搭建过程图文详解
2022-01-17 14:04:08
如何让C#、VB.NET实现复杂的二进制操作
2023-10-14 19:22:52
Spring Boot2.x如何自定义Endpoint
2023-03-05 18:04:12
自己动手写的mybatis分页插件(极其简单好用)
2023-11-01 18:12:09
Java实现微信发红包
2021-07-22 05:11:07
Spring的Aware接口你知道多少
2023-01-19 12:44:20
浅谈HashMap、HashTable的key和value是否可为null
2022-03-12 19:57:47
Java使用excel工具类导出对象功能示例
2022-07-04 23:06:50
WinForm实现自定义右下角提示效果的方法
2023-01-19 08:19:57
SpringBoot项目部署到腾讯云的实现步骤
2023-01-01 16:58:55
Java8方法引用及构造方法引用原理实例解析
2022-07-24 08:21:22
详解Spring如何解析占位符
2023-11-27 12:44:46