怎么在执行sql语句报错的时候输出执行的sql语句啊 返回
柠檬不萌 发布于2025/7/22
我这里输出的语句 只能输出带参数的语句
using Serilog;
using SqlSugar;
public class SqlHelper : IDisposable
{
private const bool IsAutoCloseConnection = true;
private const string ConfigId = "Host";
public SqlSugarClient Client { get; set; }
public IAdo Ado { get; set; }
public SqlHelper()
{
//
Client = CreateClient();
var Context = Client.GetConnection(ConfigId);
Ado = Context.Ado;
}
public void Dispose()
{
Client.Close();
Client.Dispose();
}
private static SqlSugarClient CreateClient()
{
return new SqlSugarClient(new List<ConnectionConfig>()
{
new ConnectionConfig()
{
ConfigId = ConfigId,
DbType = DbType.SqlServer,
ConnectionString = InterfaceConfig.LisHost,
IsAutoCloseConnection = IsAutoCloseConnection,
},
},
_db =>
{
_db.Aop.OnError = ( exp ) =>
{
Log.Information($"执行Sql语句出错\r\n{exp.Sql.ToSqlValue()}");
};
}
);
}
}
热忱回答(3)
-
fate sta VIP0
2025/7/22exp.Sql
exp.Paramterss
传给这个方法
Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer,sql,pars))
这种方式只适合调式或者异常使用对性能有一些影响
0 回复 -
柠檬不萌 VIP0
2025/7/231
0 回复 -
柠檬不萌 VIP0
2025/7/23@fate sta:
private static SqlSugarClient CreateClient()
{
return new SqlSugarClient(new List<ConnectionConfig>()
{
new ConnectionConfig()
{
ConfigId = ConfigId,
DbType = DbType.SqlServer,
ConnectionString = InterfaceConfig.LisHost,
IsAutoCloseConnection = IsAutoCloseConnection,
},
},
_db =>
{
_db.Aop.OnError = ( exp ) =>
{
Console.WriteLine(UtilMethods.GetSqlString(DbType.SqlServer, exp.Sql, exp.Parametres));
throw exp;
};
}
);
}
这样写会报错 因为 exp.Parametres是object类型 ,GetSqlString 第三个参数要求是 SugarParameter[] 类型
0 回复