解决数据库并发问题
This commit is contained in:
@@ -525,8 +525,6 @@ namespace JinYuan.ControlCenters
|
|||||||
|
|
||||||
LogHelper.Instance.WriteLog($"[{logName}] 反馈PLC结果:{string.Join(",", listRes)}", logName);
|
LogHelper.Instance.WriteLog($"[{logName}] 反馈PLC结果:{string.Join(",", listRes)}", logName);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bool writeResult = CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
bool writeResult = CommonMethods.plcDevices[pf.PlcNum - 1].WriteInt16(pf.VarList[0].VarAddress, 0);
|
||||||
LogHelper.Instance.WriteLog($"[{logName}] 清0结果:{writeResult}", logName);
|
LogHelper.Instance.WriteLog($"[{logName}] 清0结果:{writeResult}", logName);
|
||||||
#endregion
|
#endregion
|
||||||
|
|||||||
@@ -357,20 +357,27 @@ namespace JinYuan.DAL
|
|||||||
/// <exception cref="Exception"></exception>
|
/// <exception cref="Exception"></exception>
|
||||||
public Task<T> QueryAsync<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
public Task<T> QueryAsync<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||||||
{
|
{
|
||||||
try
|
return QueryAsyncImpl<T>(whereLambda);
|
||||||
{
|
|
||||||
ISugarQueryable<T> up = SqlSugarDb.Queryable<T>().With(SqlWith.NoLock);
|
|
||||||
if (whereLambda != null)
|
|
||||||
{
|
|
||||||
up = up.Where(whereLambda);
|
|
||||||
}
|
|
||||||
return up.FirstAsync();
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new Exception(ex.Message);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async Task<T> QueryAsyncImpl<T>(Expression<Func<T, bool>> whereLambda = null) where T : class, new()
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||||||
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||||||
|
{
|
||||||
|
ConnectionString = config.ConnectionString,
|
||||||
|
DbType = config.DbType,
|
||||||
|
IsAutoCloseConnection = true
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
var up = db.Queryable<T>().With(SqlWith.NoLock);
|
||||||
|
if (whereLambda != null) up = up.Where(whereLambda);
|
||||||
|
return await up.FirstAsync();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -505,7 +512,7 @@ namespace JinYuan.DAL
|
|||||||
{
|
{
|
||||||
up = up.Where(whereLambda);
|
up = up.Where(whereLambda);
|
||||||
}
|
}
|
||||||
if (sort != "")
|
if (!string.IsNullOrEmpty(sort))
|
||||||
{
|
{
|
||||||
var orderBys = ParseOrderBy(sort, byType);
|
var orderBys = ParseOrderBy(sort, byType);
|
||||||
up = up.OrderBy(orderBys).Take(Take);
|
up = up.OrderBy(orderBys).Take(Take);
|
||||||
@@ -537,7 +544,7 @@ namespace JinYuan.DAL
|
|||||||
{
|
{
|
||||||
up = up.Where(whereLambda);
|
up = up.Where(whereLambda);
|
||||||
}
|
}
|
||||||
if (sort != "")
|
if (!string.IsNullOrEmpty(sort))
|
||||||
{
|
{
|
||||||
var orderBys = ParseOrderBy(sort, byType);
|
var orderBys = ParseOrderBy(sort, byType);
|
||||||
up = up.OrderBy(orderBys);
|
up = up.OrderBy(orderBys);
|
||||||
@@ -876,14 +883,18 @@ namespace JinYuan.DAL
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = await SqlSugarDb.Insertable(entity).With(SqlWith.UpdLock).ExecuteCommandAsync() > 0;
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||||||
return result;
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
throw new Exception(ex.Message);
|
ConnectionString = config.ConnectionString,
|
||||||
|
DbType = config.DbType,
|
||||||
|
IsAutoCloseConnection = true
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
return await db.Insertable(entity).With(SqlWith.UpdLock).ExecuteCommandAsync() > 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -912,15 +923,18 @@ namespace JinYuan.DAL
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
//var result = await SqlSugarDb.Insertable(entitys).With(SqlWith.UpdLock).ExecuteCommandAsync() > 0;
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||||||
var result = await SqlSugarDb.Insertable(entitys).ExecuteCommandAsync() > 0;
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||||||
return result;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
throw new Exception(ex.Message);
|
ConnectionString = config.ConnectionString,
|
||||||
|
DbType = config.DbType,
|
||||||
|
IsAutoCloseConnection = true
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
return await db.Insertable(entitys).ExecuteCommandAsync() > 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
@@ -1104,23 +1118,21 @@ namespace JinYuan.DAL
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
IUpdateable<T> up = SqlSugarDb.Updateable<T>().SetColumns(update);
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||||||
if (where != null)
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||||||
{
|
{
|
||||||
up = up.Where(where);
|
ConnectionString = config.ConnectionString,
|
||||||
}
|
DbType = config.DbType,
|
||||||
if (isLock)
|
IsAutoCloseConnection = true
|
||||||
|
}))
|
||||||
{
|
{
|
||||||
up = up.With(SqlWith.UpdLock);
|
var up = db.Updateable<T>().SetColumns(update);
|
||||||
|
if (where != null) up = up.Where(where);
|
||||||
|
if (isLock) up = up.With(SqlWith.UpdLock);
|
||||||
|
return await up.ExecuteCommandAsync();
|
||||||
}
|
}
|
||||||
var result = await up.ExecuteCommandAsync();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
|
|
||||||
throw new Exception(ex.Message);
|
|
||||||
}
|
}
|
||||||
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1178,30 +1190,22 @@ namespace JinYuan.DAL
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
// 每次获取新的 SqlSugarScope 实例,避免多线程冲突
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||||||
// IsAutoCloseConnection = true 已配置,无需using语句
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||||||
|
|
||||||
IUpdateable<T> up = SqlSugarDb.Updateable<T>(entity);
|
|
||||||
if (IgnoreColumns != null)
|
|
||||||
{
|
{
|
||||||
up = up.UpdateColumns(IgnoreColumns);
|
ConnectionString = config.ConnectionString,
|
||||||
}
|
DbType = config.DbType,
|
||||||
if (where != null)
|
IsAutoCloseConnection = true
|
||||||
|
}))
|
||||||
{
|
{
|
||||||
up = up.Where(where);
|
var up = db.Updateable<T>(entity);
|
||||||
|
if (IgnoreColumns != null) up = up.UpdateColumns(IgnoreColumns);
|
||||||
|
if (where != null) up = up.Where(where);
|
||||||
|
if (isLock) up = up.With(SqlWith.UpdLock);
|
||||||
|
return await up.ExecuteCommandAsync();
|
||||||
}
|
}
|
||||||
if (isLock)
|
|
||||||
{
|
|
||||||
up = up.With(SqlWith.UpdLock);
|
|
||||||
}
|
}
|
||||||
var result = await up.ExecuteCommandAsync();
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||||||
return result;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
throw new Exception(ex.Message);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1535,20 +1539,24 @@ namespace JinYuan.DAL
|
|||||||
/// <param name="isLock"></param>
|
/// <param name="isLock"></param>
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
/// <exception cref="Exception"></exception>
|
/// <exception cref="Exception"></exception>
|
||||||
public Task<int> DeleteAsync<T>(Expression<Func<T, bool>> where, bool isLock = true) where T : class, new()
|
public async Task<int> DeleteAsync<T>(Expression<Func<T, bool>> where, bool isLock = true) where T : class, new()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
var result = isLock ?
|
var config = SqlSugarDb.CurrentConnectionConfig;
|
||||||
SqlSugarDb.Deleteable<T>().Where(where).With(SqlWith.RowLock).ExecuteCommandAsync()
|
using (var db = new SqlSugarClient(new ConnectionConfig()
|
||||||
: SqlSugarDb.Deleteable<T>().Where(where).ExecuteCommandAsync();
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
|
ConnectionString = config.ConnectionString,
|
||||||
throw new Exception(ex.Message);
|
DbType = config.DbType,
|
||||||
|
IsAutoCloseConnection = true
|
||||||
|
}))
|
||||||
|
{
|
||||||
|
return isLock
|
||||||
|
? await db.Deleteable<T>().Where(where).With(SqlWith.RowLock).ExecuteCommandAsync()
|
||||||
|
: await db.Deleteable<T>().Where(where).ExecuteCommandAsync();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex) { throw new Exception(ex.Message); }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -965,31 +965,6 @@ namespace LargeSquareOne
|
|||||||
{
|
{
|
||||||
new FrmMsgBoxWithAck("批量复投条码个数>8, 请单个复投", "提示").ShowDialog();
|
new FrmMsgBoxWithAck("批量复投条码个数>8, 请单个复投", "提示").ShowDialog();
|
||||||
}
|
}
|
||||||
//if (CommonMethods.isRepeat)
|
|
||||||
//{
|
|
||||||
// for (int i = 0; i < CommonMethods.repeatCodeList.Count; i++)
|
|
||||||
// {
|
|
||||||
// string code = CommonMethods.repeatCodeList[i];//取出队列数据
|
|
||||||
// if (code.Length > 11) //手持扫码枪的条码格式结尾带字符,到时候根据那个结尾字符判断
|
|
||||||
// {
|
|
||||||
// if (counter > 8) counter = 1; // 循环计数(1-8)
|
|
||||||
// // 将计数值写入PLC的D9050地址(16位整数)
|
|
||||||
// CommonMethods.plcDevices[0]?.WriteValue("D9050", counter, PLC.DataType.Int);
|
|
||||||
// // 写入条码到D9000
|
|
||||||
// JYResult result1 = CommonMethods.plcDevices[0].WriteValue(
|
|
||||||
// "D9000",
|
|
||||||
// code,
|
|
||||||
// PLC.DataType.String
|
|
||||||
// );
|
|
||||||
// Thread.Sleep(150);
|
|
||||||
// if (result1 == null)
|
|
||||||
// {
|
|
||||||
// CommonMethods.AddDataMonitorLog(2, "条码复投失败".Translated() + ":" + code);
|
|
||||||
// }
|
|
||||||
// counter++; // 计数器递增
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
else if (checkBox4.Checked)//已包蓝膜
|
else if (checkBox4.Checked)//已包蓝膜
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user