geboshi_V1/LeatherProject/LeatherApp/Utils/DBUtils.cs

101 lines
3.7 KiB
C#
Raw Normal View History

2024-03-07 14:03:22 +08:00
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
2024-03-07 14:03:22 +08:00
namespace LeatherApp.Utils
{
public class DBUtils
{
private static SqlSugarClient db;
//public static dbBackup()
//{
// //mysqldump -uroot-p123456 mydb > /data/mysqlDump/mydb.sql
// //mysqldump - uroot - p123456 mydb > / data / mysqlDump / mydb.sql
//}
public static SqlSugarClient getErpDBCon(SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
{
if (string.IsNullOrEmpty(Config.ErpDBConStr))
return null;
if (db != null) return db;
db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = Config.ErpDBConStr,
DbType = dbType,
IsAutoCloseConnection = true
},
db =>
{
db.Aop.OnLogExecuting = (sql, pars) =>
{
Console.WriteLine(sql);//输出sql,查看执行sql 性能无影响
//获取原生SQL推荐 5.1.4.63 性能OK
//UtilMethods.GetNativeSql(sql,pars)
//获取无参数化SQL 对性能有影响特别大的SQL参数多的调试使用
//UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
};
});
return db;
}
public static DataTable execSql(string sql, List<SugarParameter> parameters, SqlSugar.DbType dbType = SqlSugar.DbType.SqlServer)
{
if (!Config.OracleDB)
{
//查询表的所有
var mydb = getErpDBCon(dbType);
if (mydb == null)
{
MessageBox.Show($"数据库连接失败:{sql}");
return null;
}
if (!mydb.Ado.IsValidConnection())
mydb.Ado.Open();
return mydb.Ado.GetDataTable(sql, parameters);
}
else
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.Oracle,
ConnectionString = Config.ErpDBConStr,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (tsql, p) =>
{
Console.WriteLine(tsql);
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
});
//var dt = db.Ado.GetDataTable(sql);
//if (dt != null)
//{
// for (int i = 0; i < dt.Rows.Count; i++)
// {
// Console.WriteLine($"PJXTBH{dt.Rows[i]["PJXTBH"]}WPH{dt.Rows[i]["WPH"]}WPMC{dt.Rows[i]["WPMC"]}SL{dt.Rows[i]["SL"]}PH{dt.Rows[i]["PH"]}JH{dt.Rows[i]["JH"]}");
// }
//}
2024-03-07 14:03:22 +08:00
string sqlstr = $"select * from tb_qc_prodinfo where PJXTBH='{parameters[0].Value}'";
var dt2 = db.Ado.GetDataTable(sqlstr);
dt2.Columns.RemoveAt(0);
dt2.Columns.RemoveAt(0);
return dt2;
//var dt2 = db.Ado.GetDataTable(sqlstr);
}
2024-03-07 14:03:22 +08:00
}
}
}