using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data.OracleClient;
using CK.SCP.Models;
namespace ChangKeTec.Wms.Controller.Report
{
public static class DBControllerByOracle
{
#region 建立数据库连接
///
/// 获取数据库连接
///
public static OracleConnection GetConnection()
{
DbSetting setting = GlobalConfig.qiMingERPDatabase;//中间库数据库
OracleConnection myCon = new OracleConnection($"Data Source={setting.数据库名称};user={setting.用户名};password={setting.密码}");
return myCon;
}
#endregion
#region 执行OracleConnection命令
///
/// 执行数据库更新,插入操作
///
public static void ExecuteNonQuery(string p_sql, OracleParameter[] p_par = null)
{
using (OracleConnection oraclecon = DBControllerByOracle.GetConnection())
{
oraclecon.Open();
OracleCommand mycom = new OracleCommand(p_sql, oraclecon);
if (p_par != null)
{
foreach (OracleParameter parm in p_par)
{
mycom.Parameters.Add(parm);
}
}
mycom.CommandTimeout = 720;
mycom.ExecuteNonQuery();
}
}
///
/// 执行批量更新,插入操作
///
public static bool ExecuteTransaction(List p_sql, Dictionary p_dic = null)
{
using (OracleConnection oraclecon = DBControllerByOracle.GetConnection())
{
bool temp = true;
oraclecon.Open();
System.Data.OracleClient.OracleTransaction trn = oraclecon.BeginTransaction();
try
{
for (int i = 0; i < p_sql.Count; i++)
{
OracleCommand mycom = new OracleCommand(p_sql[i], oraclecon, trn);
if (p_dic != null && p_dic.ContainsKey(i.ToString()))
{
OracleParameter[] p_par = p_dic[i.ToString()] as OracleParameter[];
foreach (OracleParameter parm in p_par)
{
mycom.Parameters.Add(parm);
}
}
mycom.ExecuteNonQuery();
}
trn.Commit();
return temp;
}
catch
{
trn.Rollback();
temp = false;
return temp;
}
finally
{
if (p_sql.Count > 0) p_sql.Clear();
if (p_dic != null) p_dic.Clear();
oraclecon.Close();
oraclecon.Dispose();
}
}
}
#endregion
#region 获取DataTable数据源对象
public static DataTable GetDataTable(string p_sql, OracleParameter[] p_par = null)
{
using (OracleConnection oraclecon = DBControllerByOracle.GetConnection())
{
OracleCommand mycom = new OracleCommand(p_sql, oraclecon);
if (p_par != null)
{
foreach (OracleParameter parm in p_par)
{
mycom.Parameters.Add(parm);
}
}
mycom.CommandTimeout = 300;
OracleDataAdapter sqlda = new OracleDataAdapter(mycom);
DataSet myds = new DataSet();
sqlda.Fill(myds);
return myds.Tables[0];
}
}
#endregion
}
}