在mysql数据库中使用excel表格插入数据时,不支持嵌套事务
本文关键字:数据 插入 嵌套事务 不支持 表格 excel 数据库 mysql | 更新日期: 2023-09-27 18:10:50
当我使用实体框架使用excel表格将数据插入MySQL时,这里我想处理每个记录在循环中的异常所以,我在循环中使用了try——catch
enter code here
foreach (DataRow dr in ds.Tables[0].Rows)
{
outstanding_master master = new outstanding_master();
{
//log.Debug("This a test debug message");
master.LoanID = dr["LoanID"].ToString();
master.Name = dr["Name"].ToString();
master.PhoneNumber = Convert.ToInt64(dr["PhoneNumber"].ToString());
master.Address = dr["Address"].ToString();
master.ServiceProviderID = Convert.ToInt32(dr
["ServiceProviderID"].ToString());
master.TotalDue = dr["TotalDue"].ToString();
master.LastPaidAmount = dr["LastPaidAmount"].ToString();
master.LastPaidDate = Convert.ToDateTime(dr["LastPaidDate"].ToString());
master.OutStandingDescription = dr["OutStandingDescription"].ToString();
master.longitude = dr["longitude"].ToString();
master.latitude = dr["latitude"].ToString();
db.outstanding_master.Add(master);
try
{
db.SaveChanges();
log.Info("Record saved successfully with LoanID: " + master.LoanID);
}
catch (Exception ex)
{
log.Info("Error saving record with LoanID: " + dr["LoanID"].ToString() + " Exception(): " + ex.InnerException.InnerException);
var entry = db.Entry(master);
if (entry.State == EntityState.Added)
{
((System.Data.Entity.Infrastructure.IObjectContextAdapter)db).ObjectContext.DeleteObject(master);
}
} } }
这里,当未完成的对象是附件,如果没有异常,它应该插入到数据库中,如果没有显示异常,并再次循环下一个事务,并分离上一个记录对象。
这里我的问题是当我插入3条记录没有任何异常工作良好,如果两个记录失败,最后3个记录也不例外,它应该插入工作良好但是如果第一个记录插入,第二个记录失败,出现一些异常(重复键),第三个记录也失败,出现以下异常,但应该插入((异常:不支持嵌套事务)
请尝试下面的链接它可能是由事务中使用的2个不同的连接引起的。你应该手动控制连接:下面的解决方案链接可能会对你有所帮助
在Entity Framework 4.0中"不支持嵌套事务";显示错误?