geboshi_V1/LeatherProject/LeatherApp/Utils/DBUtils.cs

151 lines
6.3 KiB
C#
Raw Normal View History

using Newtonsoft.Json;
using SqlSugar;
2024-03-07 14:03:22 +08:00
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 (string.IsNullOrEmpty(Config.ErpDBType) || Config.ErpDBType == "SqlServer")
{
//查询表的所有
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 if (Config.ErpDBType == "Oracle")
{
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"]}");
// }
//}
string sqlstr = $"{sql}'{parameters[0].Value}'";
//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);
}
else if (Config.ErpDBType == "PostgreSQL")
{
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
DbType = SqlSugar.DbType.PostgreSQL,
ConnectionString = Config.ErpDBConStr,
IsAutoCloseConnection = true,
AopEvents = new AopEvents
{
OnLogExecuting = (tsql, p) =>
{
Console.WriteLine(tsql);
Console.WriteLine(string.Join(",", p?.Select(it => it.ParameterName + ":" + it.Value)));
}
}
});
//string sqlstr = $"select * from mingxin_prod where PJXTBH='{parameters[0].Value}'";
//var dt2 = db.Ado.GetDataTable(sqlstr);
//dt2.Columns.RemoveAt(0);
//dt2.Columns.RemoveAt(0);
string sqlstr = $"select id,current_qty,batch_no,goods_code,material_id from mfg_material_goods where id={parameters[0].Value}";
var dt2 = db.Ado.GetDataTable(sqlstr);
sqlstr = $"select id,material_code,material_name from base_material where id={dt2.Rows[0]["material_id"]}";
var dt3 = db.Ado.GetDataTable(sqlstr);
DataTable dt = new DataTable(); //建立个数据表WPMC,SL,PH,JH
dt.Columns.Add(new DataColumn("WPMC", typeof(string)));//在表中添加string类型的列
dt.Columns.Add(new DataColumn("SL", typeof(string)));//在表中添加string类型的列
dt.Columns.Add(new DataColumn("PH", typeof(string)));//在表中添加string类型的列
dt.Columns.Add(new DataColumn("JH", typeof(string)));//在表中添加string类型的列
dt.Columns.Add(new DataColumn("ERPID", typeof(string)));//在表中添加string类型的列
DataRow dr;//行
dr = dt.NewRow();
dr["WPMC"] = dt3.Rows[0]["material_name"];
dr["SL"] = dt2.Rows[0]["current_qty"];
dr["PH"] = dt2.Rows[0]["batch_no"];
dr["JH"] = dt2.Rows[0]["goods_code"];
dr["ERPID"] = dt3.Rows[0]["material_code"];
dt.Rows.Add(dr);//在表的对象的行里添加此行
return dt;
}
else
return null;
2024-03-07 14:03:22 +08:00
}
}
}