当前位置:
首页 > Python基础教程 >
-
C# MySql Transaction Async
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Configuration; using MySql.Data.MySqlClient; using System.Data.Common; using ConsoleApplication22.Model; using System.Reflection; namespace ConsoleApplication22 { class Program { static void Main(string[] args) { ReadAsyncDemo(); Console.ReadLine(); } static async void ReadAsyncDemo() { string selectSQL = "select * from country"; IList<Country> countryList = await MySqlRead2Async<Country>(selectSQL); } static string MySqlConnectionString = ConfigurationManager.ConnectionStrings["MySqlConnectionString"].ConnectionString; static async void MySqlReadAsync(string readSQL,Dictionary<string,object> parametersDic=null) { using (MySqlConnection conn = GetMySqlConnection()) { if (conn.State != ConnectionState.Open) { conn.Open(); } using (MySqlCommand cmd = new MySqlCommand(readSQL, conn)) { using (DbDataReader dataReader = await cmd.ExecuteReaderAsync()) { StringBuilder selectBuilder = new StringBuilder(); while (await dataReader.ReadAsync()) { for(int i=0;i<dataReader.FieldCount;i++) { selectBuilder.Append(dataReader[i]+"\t"); } selectBuilder.AppendLine(); } Console.WriteLine(selectBuilder.ToString()); } } } } static async Task<IList<T>> MySqlRead2Async<T>(string selectSQL,Dictionary<string,object> parametersDic=null)where T:class { IList<T> dataList = new List<T>(); using (MySqlConnection conn = GetMySqlConnection()) { if (conn.State != ConnectionState.Open) { conn.Open(); } using(MySqlCommand selectCmd=new MySqlCommand(selectSQL, conn)) { using (DbDataReader dataReaderAsync = await selectCmd.ExecuteReaderAsync()) { if (dataReaderAsync.HasRows) { DataTable dt = new DataTable(); dt.Load(dataReaderAsync); dataList = dt.ToDataList<T>(); } } } } return dataList; } static async Task<int> MySqlWriteAsync(string writeSQL,Dictionary<string,object> parametersDic=null) { int executeResult = -1; using (MySqlConnection conn = GetMySqlConnection()) { if (conn.State != ConnectionState.Open) { conn.Open(); } using (MySqlCommand cmd = new MySqlCommand(writeSQL, conn)) { using (MySqlTransaction myTrans = await conn.BeginTransactionAsync()) { try { if (parametersDic != null && parametersDic.Any()) { foreach (var pDic in parametersDic) { cmd.Parameters.AddWithValue(pDic.Key, pDic.Value); } } cmd.Transaction = myTrans; executeResult = await cmd.ExecuteNonQueryAsync(); myTrans.Commit(); } catch (Exception ex) { myTrans.Rollback(); Console.WriteLine(ex.Message); } } } } return executeResult; } static MySqlConnection GetMySqlConnection() { MySqlConnection conn = new MySqlConnection(); conn.ConnectionString = MySqlConnectionString; return conn; } } static class ExtendClass { public static List<T> ToDataList<T>(this DataTable dt) { var list = new List<T>(); var plist = new List<PropertyInfo>(typeof(T).GetProperties()); foreach (DataRow item in dt.Rows) { T s = Activator.CreateInstance<T>(); for (int i = 0; i < dt.Columns.Count; i++) { PropertyInfo info = plist.Find(p => p.Name == dt.Columns[i].ColumnName); if (info != null) { try { if (!Convert.IsDBNull(item[i])) { object v = null; if (info.PropertyType.ToString().Contains("System.Nullable")) { v = Convert.ChangeType(item[i], Nullable.GetUnderlyingType(info.PropertyType)); } else { v = Convert.ChangeType(item[i], info.PropertyType); } info.SetValue(s, v, null); } } catch (Exception ex) { throw new Exception("字段[" + info.Name + "]转换出错," + ex.Message); } } } list.Add(s); } return list; } } }
static async Task<int> MySqlWriteAsync(string writeSQL,Dictionary<string,object> parametersDic=null) { int executeResult = -1; using (MySqlConnection conn = GetMySqlConnection()) { if (conn.State != ConnectionState.Open) { conn.Open(); } using (MySqlCommand cmd = new MySqlCommand(writeSQL, conn)) { using (MySqlTransaction myTrans = await conn.BeginTransactionAsync()) { try { if (parametersDic != null && parametersDic.Any()) { foreach (var pDic in parametersDic) { cmd.Parameters.AddWithValue(pDic.Key, pDic.Value); } } cmd.Transaction = myTrans; executeResult = await cmd.ExecuteNonQueryAsync(); myTrans.Commit(); } catch (Exception ex) { myTrans.Rollback(); Console.WriteLine(ex.Message); } } } } return executeResult; }
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式