C# MySQL,Dapper Trans,list
阅读原文时间:2023年07月09日阅读:3

static async Task MySQLTransDemo()
{

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

//The object matched type such as class must have the column fields in table
List objList = new List();
//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);
}
}