C# MySQL,Dapper Trans,list

static async Task MySQLTransDemo()
{

try
{
using(dbConnection)
{
dbConnection.Open();

//The object matched type such as class must have the column fields in table
List<object> objList = new List<object>();
//assign values to objList;
//to-do
//objList=GetObjList();
using(IDbTransaction trans=dbConnection.BeginTransaction())
{
//match columns and values respectively.
string transSQL = "insert into table_Name(column1,column2) values(@column1,@column2)";
//In this example,this objList collection's item object must have column1 and column2 properties.
var transNum = await dbConnection.ExecuteAsync(transSQL, objList, trans);
trans.Commit();
if(transNum<0)
{
trans.Rollback();
}
}
}
}

catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
}

posted @ 2019-08-09 16:03  FredGrit  阅读(283)  评论(0编辑  收藏  举报