@@ -54,6 +54,7 @@ | |||||
<Reference Include="System.Xml" /> | <Reference Include="System.Xml" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Compile Include="InsturmentData\DmServerInstrumentData.cs" /> | |||||
<Compile Include="InsturmentData\KingbaseInstrumentData.cs" /> | <Compile Include="InsturmentData\KingbaseInstrumentData.cs" /> | ||||
<Compile Include="InsturmentData\PostgreSQLServerInstrumentData.cs" /> | <Compile Include="InsturmentData\PostgreSQLServerInstrumentData.cs" /> | ||||
<Compile Include="TargetCnasData\DB2CNASDataOperation.cs" /> | <Compile Include="TargetCnasData\DB2CNASDataOperation.cs" /> | ||||
@@ -151,6 +151,7 @@ | |||||
<Reference Include="System.Xml" /> | <Reference Include="System.Xml" /> | ||||
</ItemGroup> | </ItemGroup> | ||||
<ItemGroup> | <ItemGroup> | ||||
<Compile Include="DAL\DmDAL.cs" /> | |||||
<Compile Include="DAL\Helper\DamengHelper.cs" /> | <Compile Include="DAL\Helper\DamengHelper.cs" /> | ||||
<Compile Include="DAL\Helper\KingbaseHelper.cs" /> | <Compile Include="DAL\Helper\KingbaseHelper.cs" /> | ||||
<Compile Include="DAL\Helper\PostgreSQLHelper.cs" /> | <Compile Include="DAL\Helper\PostgreSQLHelper.cs" /> | ||||
@@ -180,6 +181,7 @@ | |||||
<Compile Include="BaseReadFileMode.cs" /> | <Compile Include="BaseReadFileMode.cs" /> | ||||
<Compile Include="ReadFileSecond.cs" /> | <Compile Include="ReadFileSecond.cs" /> | ||||
<Compile Include="Service\KingbaseDBService.cs" /> | <Compile Include="Service\KingbaseDBService.cs" /> | ||||
<Compile Include="Service\DmDBService.cs" /> | |||||
<Compile Include="Service\PostgreSqlDBService.cs" /> | <Compile Include="Service\PostgreSqlDBService.cs" /> | ||||
<Compile Include="Service\SqliteDBService.cs" /> | <Compile Include="Service\SqliteDBService.cs" /> | ||||
<Compile Include="Service\SqlServerDBService.cs" /> | <Compile Include="Service\SqlServerDBService.cs" /> | ||||
@@ -0,0 +1,571 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Data; | |||||
using CnasSynchronousCommon; | |||||
using CnasSynchrousModel; | |||||
using System.Reflection; | |||||
using Dm; | |||||
namespace CnasSynchronusDAL | |||||
{ | |||||
public class DmDAL | |||||
{ | |||||
public void CreateConnectString(string strIP, string strPort, string strName, string strUser, string strPwd) | |||||
{ | |||||
DamengHelper.InitConnectionString(strIP, strPort, strName, strUser, strPwd); | |||||
} | |||||
public void CreateConnectString(string strConnectString) | |||||
{ | |||||
DamengHelper.InitConnectionString(strConnectString); | |||||
} | |||||
//获取所有表单名称 | |||||
public DataTable GetTableNames(string strName) | |||||
{ | |||||
DataTable dt = new DataTable(); | |||||
string strSql = string.Format("SELECT table_name as TABNAME FROM user_tables WHERE owner='{0}'", strName); | |||||
try | |||||
{ | |||||
dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return dt; | |||||
} | |||||
/// <summary> | |||||
/// 获取某表的表结构 | |||||
/// </summary> | |||||
public DataTable GetTableStruct(string strTableName, string strViewName, string strViewSql) | |||||
{ | |||||
DataTable dt = new DataTable(); | |||||
if (strTableName.Length <= 0) return dt; | |||||
string strSql = ""; | |||||
if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName)) | |||||
strSql = strViewSql + " where 0=1"; | |||||
else | |||||
strSql = string.Format("SELECT * FROM {0} Where 0=1", strTableName); | |||||
try | |||||
{ | |||||
dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return dt; | |||||
} | |||||
/// <summary> | |||||
/// 获取某表的表结构和类型长度 | |||||
/// </summary> | |||||
public DataTable GetTableTypeAndLenth(string strTableName) | |||||
{ | |||||
DataTable dt = new DataTable(); | |||||
string strSql = string.Format(@"SELECT | |||||
COLUMN_NAME AS 'ColumnName', | |||||
NULLABLE AS 'IsNullable', | |||||
DATA_TYPE AS 'DataType', | |||||
DATA_LENGTH AS 'CharMaxLenth', | |||||
DATA_LENGTH AS 'CharOcterLenth', | |||||
DATA_PRECISION AS 'NumericPrecision', | |||||
DATA_SCALE AS 'NumericScale' | |||||
FROM USER_TAB_COLUMNS | |||||
WHERE TABLE_NAME = '{0}'", strTableName); | |||||
try | |||||
{ | |||||
dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return dt; | |||||
} | |||||
/// <summary> | |||||
/// 逐行批量插入数据 | |||||
/// </summary> | |||||
public int InsertCnasData(DataTable dt, List<SyncParamasInfo> syncParamasInfos, List<CnasConditionMapValue> lstFixedValue, string strInsumentColumn) | |||||
{ | |||||
int iReturn = 0; | |||||
if (dt.Rows.Count <= 0) return 0; | |||||
try | |||||
{ | |||||
//获取唯一健组(关键字段) | |||||
var query = from p in syncParamasInfos | |||||
where p.IfPrimaryKey == true | |||||
select new | |||||
{ | |||||
p.TargetField | |||||
}; | |||||
List<string> lstKeyColumns = new List<string>(); | |||||
foreach (var item in query) | |||||
{ | |||||
lstKeyColumns.Add(item.TargetField); | |||||
} | |||||
//构建SQL语句 | |||||
string strSql_part1 = ""; | |||||
string strSql_part2 = ""; | |||||
List<string> lstColumnName = new List<string>(); | |||||
foreach (var item in syncParamasInfos) | |||||
{ | |||||
if (!lstColumnName.Contains(item.TargetField.ToLower())) | |||||
{ | |||||
strSql_part1 += item.TargetField + ","; | |||||
strSql_part2 += string.Format(":{0},", item.TargetField.ToLower()); | |||||
lstColumnName.Add(item.TargetField.ToLower()); | |||||
} | |||||
} | |||||
//固定列处理 | |||||
if (lstFixedValue != null) | |||||
{ | |||||
foreach (var cnasfield in lstFixedValue) | |||||
{ | |||||
if (cnasfield.TableName != syncParamasInfos[0].TargetTable) continue; | |||||
if (!lstColumnName.Contains(cnasfield.ColumnName.ToLower())) | |||||
{ | |||||
strSql_part1 += cnasfield.ColumnName + ","; | |||||
strSql_part2 += string.Format(":{0},", cnasfield.ColumnName.ToLower()); | |||||
lstColumnName.Add(cnasfield.ColumnName.ToLower()); | |||||
} | |||||
} | |||||
} | |||||
//增加仪器编号数据 | |||||
if (!string.IsNullOrWhiteSpace(strInsumentColumn) && !lstColumnName.Contains(strInsumentColumn.ToLower())) | |||||
{ | |||||
strSql_part1 += strInsumentColumn + ","; | |||||
strSql_part2 += string.Format(":{0},", strInsumentColumn); | |||||
lstColumnName.Add(strInsumentColumn.ToLower()); | |||||
} | |||||
string strInsertSql = string.Format("insert into {0}({1}) values({2})", | |||||
syncParamasInfos[0].TargetTable, | |||||
strSql_part1.Substring(0, strSql_part1.Length - 1), | |||||
strSql_part2.Substring(0, strSql_part2.Length - 1)); | |||||
string strUpdateSql = ""; | |||||
DataTable dtSelect = new DataTable(); | |||||
foreach (DataRow dr in dt.Rows) | |||||
{ | |||||
//插入参数值 | |||||
DmParameter[] parameters = new DmParameter[lstColumnName.Count]; | |||||
int i = 0; | |||||
foreach (var item in lstColumnName) | |||||
{ | |||||
parameters[i++] = new DmParameter(item, dr[item]); | |||||
} | |||||
//检查是否存在数据 | |||||
int ifHavaValue = ExistSingleCnasData(lstKeyColumns, syncParamasInfos[0].TargetTable, dr, ref dtSelect); | |||||
if (ifHavaValue == 1) | |||||
{ | |||||
if (dtSelect.Rows.Count == 1) | |||||
{ | |||||
//比对数据是否一致 | |||||
if (!CompareObjectOperation.DataRowCompare(dtSelect.Rows[0], dr, lstColumnName)) | |||||
{ | |||||
//构造更新语句 | |||||
strUpdateSql = GetUpdateSql(lstColumnName, lstKeyColumns, syncParamasInfos[0].TargetTable, dr); | |||||
//执行UpdateSql语句 | |||||
iReturn += DamengHelper.ExecuteNonQuery(strUpdateSql, parameters); | |||||
} | |||||
else | |||||
{ | |||||
iReturn = -2; | |||||
AppLog.Info("更新时发现在数据库中相同关键字段数据一致。"); | |||||
} | |||||
} | |||||
else | |||||
{ | |||||
AppLog.Error("更新时发现在数据库中多条相同关键字段数据,请重新配置关键字段。"); | |||||
} | |||||
} | |||||
else if (ifHavaValue == 0) | |||||
{ | |||||
//执行InsertSQL语句 | |||||
iReturn += DamengHelper.ExecuteNonQuery(strInsertSql, parameters); | |||||
} | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
if (!LinkCnasTest()) | |||||
{ | |||||
iReturn = -1; //用于表示插入时无法正常数据库连接问题 | |||||
} | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return iReturn; | |||||
} | |||||
/// <summary> | |||||
/// 拼接Update语句 | |||||
/// </summary> | |||||
private string GetUpdateSql(List<string> lstColumnName, List<string> lstPrimaryColumn, string strTableName, DataRow dr) | |||||
{ | |||||
//构造关键字段条件 | |||||
string strsql_partial = ""; | |||||
foreach (var item in lstPrimaryColumn) | |||||
{ | |||||
if (dr.Table.Columns.Contains(item.ToString())) | |||||
if (dr[item.ToString()].ToString() != "") | |||||
strsql_partial += $"{item.ToString()}='{dr[item.ToString()].ToString()}' and "; | |||||
else | |||||
strsql_partial += $"({item.ToString()}='{dr[item.ToString()].ToString()}' or {item.ToString()} is null) and "; | |||||
} | |||||
//构造Update语句 | |||||
string strUpdateSql = ""; | |||||
string strsql_partial2 = ""; | |||||
foreach (var item in lstColumnName) | |||||
{ | |||||
strsql_partial2 += $"{item}=:{item},"; | |||||
} | |||||
if (strsql_partial.Length > 3 && strsql_partial2.Length > 0) | |||||
strUpdateSql = $"update {strTableName} set {strsql_partial2.Substring(0, strsql_partial2.Length - 1)} where {strsql_partial.Substring(0, strsql_partial.Length - 4)}"; | |||||
return strUpdateSql; | |||||
} | |||||
/// <summary> | |||||
/// 是否数据库中已经存在数据 | |||||
/// </summary> | |||||
private int ExistSingleCnasData(List<string> lstPrimaryColumn, string strTableName, DataRow dr, ref DataTable dtSelect) | |||||
{ | |||||
int bIfHaveValue = 0; | |||||
string strsql_partial = ""; | |||||
string strSql = ""; | |||||
foreach (var item in lstPrimaryColumn) | |||||
{ | |||||
if (dr.Table.Columns.Contains(item.ToString())) | |||||
if (dr[item.ToString()].ToString() != "") | |||||
strsql_partial += $"{item.ToString()}='{dr[item.ToString()].ToString()}' and "; | |||||
else | |||||
strsql_partial += $"({item.ToString()}='{dr[item.ToString()].ToString()}' or {item.ToString()} is null) and "; | |||||
} | |||||
if (strsql_partial.Length > 3) | |||||
strSql = $"select * from {strTableName} where {strsql_partial.Substring(0, strsql_partial.Length - 4)}"; | |||||
if (strSql != "") | |||||
{ | |||||
DataTable dt = DamengHelper.ExecuteDataTable(strSql, new DmParameter[] { }); | |||||
if (dt != null && dt.Rows.Count > 0) | |||||
{ | |||||
bIfHaveValue = 1; | |||||
dtSelect = dt; | |||||
AppLog.Info($"插入前查询时存在重复数据:{strSql}"); | |||||
} | |||||
else if (dt == null) | |||||
{ | |||||
bIfHaveValue = -1; | |||||
AppLog.Info($"插入前查询时发生了异常:{strSql}"); | |||||
} | |||||
} | |||||
return bIfHaveValue; | |||||
} | |||||
public bool LinkCnasTest() | |||||
{ | |||||
return DamengHelper.TestConnectDameng(); | |||||
} | |||||
/// <summary> | |||||
/// 获得表中该列中最大时间 | |||||
/// </summary> | |||||
public string GetMaxTimeByTableName(string strTableName, string strDateColumn, string strInstrumentColumn, string strInstrumentValue) | |||||
{ | |||||
string strReturnTime = ""; | |||||
try | |||||
{ | |||||
string strSql = $"SELECT TO_CHAR(MAX({strDateColumn}), 'YYYY-MM-DD HH24:MI:SS') FROM {strTableName}"; | |||||
if (!string.IsNullOrWhiteSpace(strInstrumentColumn) && !string.IsNullOrWhiteSpace(strInstrumentValue)) | |||||
strSql += $" WHERE {strInstrumentColumn}='{strInstrumentValue}'"; | |||||
string strDateTime = GetMaxTimeByTableName(strSql); | |||||
DateTime dateTime = DateTime.Now; | |||||
if (DateTime.TryParse(strDateTime, out dateTime)) | |||||
{ | |||||
strReturnTime = strDateTime; | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
strReturnTime = "1899-1-1"; | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return strReturnTime; | |||||
} | |||||
internal DataTable GetDataByDateColumn(string strDBName, string strViewName, string strViewSql, string strTableName, string strDateColumn, string strDate) | |||||
{ | |||||
DataTable dtReturn = new DataTable(); | |||||
try | |||||
{ | |||||
string strSql = ""; | |||||
if (strViewName == strTableName && !string.IsNullOrWhiteSpace(strViewName)) | |||||
strSql = strViewSql + $" where {strDateColumn} > TO_DATE('{strDate}', 'YYYY-MM-DD HH24:MI:SS')"; | |||||
else | |||||
strSql = $"select * from {strTableName} where {strDateColumn} > TO_DATE('{strDate}', 'YYYY-MM-DD HH24:MI:SS')"; | |||||
DataTable dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
Dictionary<string, string> dictFiled = GetSpecialOperaField(strDBName, strTableName); | |||||
if (dictFiled.Count > 0) | |||||
dtReturn = DateAndTimeTypeOpera(dt, dictFiled); | |||||
else | |||||
dtReturn = dt; | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
throw ex; | |||||
} | |||||
return dtReturn; | |||||
} | |||||
internal Dictionary<string, DataTable> GetAllTableNameAndStructure(string strDBName) | |||||
{ | |||||
Dictionary<string, DataTable> dictTables = new Dictionary<string, DataTable>(); | |||||
try | |||||
{ | |||||
DataTable TablesName = GetTableNames(strDBName); | |||||
foreach (DataRow dr in TablesName.Rows) | |||||
{ | |||||
string strTableName = dr[0].ToString(); | |||||
dictTables.Add(strTableName.ToUpper(), GetTableStruct(strTableName, "", "")); | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return dictTables; | |||||
} | |||||
private string GetMaxTimeByTableName(string strSql) | |||||
{ | |||||
string strDateTime = ""; | |||||
try | |||||
{ | |||||
DataTable dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
if (dt != null && dt.Rows.Count == 1) | |||||
{ | |||||
strDateTime = dt.Rows[0][0].ToString(); | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
if (!LinkCnasTest()) | |||||
{ | |||||
strDateTime = "1899-1-1"; | |||||
} | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return strDateTime; | |||||
} | |||||
/// <summary> | |||||
/// 获取特殊处理字段 | |||||
/// </summary> | |||||
private Dictionary<string, string> GetSpecialOperaField(string strDBName, string strTableName) | |||||
{ | |||||
Dictionary<string, string> DictFiled = new Dictionary<string, string>(); | |||||
try | |||||
{ | |||||
string sql = $"SELECT COLUMN_NAME, DATA_TYPE FROM USER_TAB_COLUMNS WHERE TABLE_NAME='{strTableName}'"; | |||||
DataTable dt = DamengHelper.ExecuteDataSet(sql).Tables[0]; | |||||
foreach (DataRow dr in dt.Rows) | |||||
{ | |||||
string dataType = dr["DATA_TYPE"].ToString().ToLower(); | |||||
if (dataType == "date" || dataType == "time" || dataType == "timestamp") | |||||
{ | |||||
DictFiled.Add(dr["COLUMN_NAME"].ToString(), dataType); | |||||
} | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return DictFiled; | |||||
} | |||||
/// <summary> | |||||
/// 处理日期时间类型数据 | |||||
/// </summary> | |||||
private DataTable DateAndTimeTypeOpera(DataTable dt, Dictionary<string, string> DictSpecialField) | |||||
{ | |||||
DataTable dtNewFormat = new DataTable(); | |||||
//添加列 | |||||
foreach (DataColumn dc in dt.Columns) | |||||
{ | |||||
if (DictSpecialField.ContainsKey(dc.ColumnName)) | |||||
{ | |||||
string strDateType = DictSpecialField[dc.ColumnName]; | |||||
switch (strDateType.ToLower()) | |||||
{ | |||||
case "date": | |||||
case "time": | |||||
case "timestamp": | |||||
dtNewFormat.Columns.Add(dc.ColumnName, typeof(string)); | |||||
break; | |||||
default: | |||||
dtNewFormat.Columns.Add(dc.ColumnName, dc.DataType); | |||||
break; | |||||
} | |||||
} | |||||
else | |||||
{ | |||||
dtNewFormat.Columns.Add(dc.ColumnName, dc.DataType); | |||||
} | |||||
} | |||||
//添加数据 | |||||
foreach (DataRow dr in dt.Rows) | |||||
{ | |||||
DataRow drNewRow = dtNewFormat.NewRow(); | |||||
foreach (DataColumn dc in dtNewFormat.Columns) | |||||
{ | |||||
if (!DictSpecialField.ContainsKey(dc.ColumnName)) | |||||
drNewRow[dc.ColumnName] = dr[dc.ColumnName]; | |||||
else | |||||
{ | |||||
if (dr[dc.ColumnName] != null && dr[dc.ColumnName].ToString() != "") | |||||
{ | |||||
switch (DictSpecialField[dc.ColumnName].ToLower()) | |||||
{ | |||||
case "date": | |||||
drNewRow[dc.ColumnName] = Convert.ToDateTime(dr[dc.ColumnName]).ToString("yyyy-MM-dd"); | |||||
break; | |||||
case "time": | |||||
drNewRow[dc.ColumnName] = Convert.ToDateTime(dr[dc.ColumnName]).ToString("HH:mm:ss"); | |||||
break; | |||||
case "timestamp": | |||||
drNewRow[dc.ColumnName] = Convert.ToDateTime(dr[dc.ColumnName]).ToString("yyyy-MM-dd HH:mm:ss"); | |||||
break; | |||||
default: | |||||
drNewRow[dc.ColumnName] = dr[dc.ColumnName]; | |||||
break; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
dtNewFormat.Rows.Add(drNewRow); | |||||
} | |||||
return dtNewFormat; | |||||
} | |||||
/// <summary> | |||||
/// 获取表数据 | |||||
/// </summary> | |||||
public DataTable GetTableData(string strSql) | |||||
{ | |||||
DataTable dt = new DataTable(); | |||||
try | |||||
{ | |||||
dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return dt; | |||||
} | |||||
/// <summary> | |||||
/// 插入表数据 | |||||
/// </summary> | |||||
public bool InsertTableData(string strSql) | |||||
{ | |||||
try | |||||
{ | |||||
return DamengHelper.ExecuteNonQuery(strSql) > 0; | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return false; | |||||
} | |||||
/// <summary> | |||||
/// 根据用户名和密码获取登录名 | |||||
/// </summary> | |||||
public string GetLoginNameByPwd(string strUser, string strPwd) | |||||
{ | |||||
string strLoginName = ""; | |||||
try | |||||
{ | |||||
string strSql = string.Format("SELECT loginname FROM sys_user WHERE username='{0}' and password='{1}'", strUser, strPwd); | |||||
DataTable dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
if (dt != null && dt.Rows.Count > 0) | |||||
{ | |||||
strLoginName = dt.Rows[0][0].ToString(); | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return strLoginName; | |||||
} | |||||
/// <summary> | |||||
/// 检查MAC地址信息 | |||||
/// </summary> | |||||
public bool CheckMacMessage(string strMac) | |||||
{ | |||||
bool bIfSuccess = false; | |||||
try | |||||
{ | |||||
string strSql = string.Format("SELECT * FROM sys_mac WHERE mac='{0}'", strMac); | |||||
DataTable dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
if (dt != null && dt.Rows.Count > 0) | |||||
{ | |||||
bIfSuccess = true; | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return bIfSuccess; | |||||
} | |||||
/// <summary> | |||||
/// 检查MAC地址信息 | |||||
/// </summary> | |||||
public bool CheckMacMessage(string strMac, string strUser) | |||||
{ | |||||
bool bIfSuccess = false; | |||||
try | |||||
{ | |||||
string strSql = string.Format("SELECT * FROM sys_mac WHERE mac='{0}' and username='{1}'", strMac, strUser); | |||||
DataTable dt = DamengHelper.ExecuteDataSet(strSql).Tables[0]; | |||||
if (dt != null && dt.Rows.Count > 0) | |||||
{ | |||||
bIfSuccess = true; | |||||
} | |||||
} | |||||
catch (Exception ex) | |||||
{ | |||||
AppLog.Error(ex.Message); | |||||
} | |||||
return bIfSuccess; | |||||
} | |||||
} | |||||
} |
@@ -74,115 +74,10 @@ namespace CnasSynchronusDAL | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 对数据库执行增删改操作,返回受影响的行数。 | |||||
/// 执行查询,返回DataSet | |||||
/// </summary> | /// </summary> | ||||
/// <param name="sql">要执行的增删改的SQL语句</param> | |||||
/// <param name="sql">要执行的查询SQL语句</param> | |||||
/// <returns></returns> | /// <returns></returns> | ||||
public static int ExecuteNonQuery(String sql, DmParameter[] cmdParams) | |||||
{ | |||||
try | |||||
{ | |||||
using (DmConnection connection = new DmConnection(mConnStr)) | |||||
{ | |||||
connection.Open(); | |||||
DmTransaction transaction = (DmTransaction)connection.BeginTransaction(); | |||||
using (DmCommand cmd = new DmCommand()) | |||||
{ | |||||
try | |||||
{ | |||||
PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, cmdParams); | |||||
int rows = cmd.ExecuteNonQuery(); | |||||
transaction.Commit(); | |||||
cmd.Parameters.Clear(); | |||||
return rows; | |||||
} | |||||
catch (DmException e1) | |||||
{ | |||||
try | |||||
{ | |||||
transaction.Rollback(); | |||||
} | |||||
catch (Exception e2) | |||||
{ | |||||
AppLog.Error(e2.Message); | |||||
throw; | |||||
} | |||||
AppLog.Error(e1.Message); | |||||
throw; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
AppLog.Error(e.Message); | |||||
throw; | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// 对达梦数据库执行操作,返回第一行第一列数据 | |||||
/// </summary> | |||||
/// <param name="sql"></param> | |||||
/// <returns></returns> | |||||
public static int ExecuteScalar(String sql) | |||||
{ | |||||
try | |||||
{ | |||||
using (DmConnection connection = new DmConnection(mConnStr)) | |||||
{ | |||||
connection.Open(); | |||||
DmTransaction transaction = (DmTransaction)connection.BeginTransaction(); | |||||
using (DmCommand cmd = new DmCommand()) | |||||
{ | |||||
try | |||||
{ | |||||
int line = 0; | |||||
PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, null); | |||||
String str = cmd.ExecuteScalar().ToString(); | |||||
transaction.Commit(); | |||||
line = Convert.ToInt32(str); | |||||
cmd.Parameters.Clear(); | |||||
return line; | |||||
} | |||||
catch (DmException e1) | |||||
{ | |||||
try | |||||
{ | |||||
transaction.Rollback(); | |||||
} | |||||
catch (Exception e2) | |||||
{ | |||||
AppLog.Error(e2.Message); | |||||
throw; | |||||
} | |||||
AppLog.Error(e1.Message); | |||||
throw; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
AppLog.Error(e.Message); | |||||
throw; | |||||
} | |||||
} | |||||
/// <summary> | |||||
/// 查询返回DataSet | |||||
/// </summary> | |||||
/// <param name="sql"></param> | |||||
/// <returns></returns> | |||||
public static DataSet ExecuteDataSet(String sql) | public static DataSet ExecuteDataSet(String sql) | ||||
{ | { | ||||
DmConnection connection = new DmConnection(mConnStr); | DmConnection connection = new DmConnection(mConnStr); | ||||
@@ -204,7 +99,6 @@ namespace CnasSynchronusDAL | |||||
DataSet ds = new DataSet(); | DataSet ds = new DataSet(); | ||||
adapter.Fill(ds); | adapter.Fill(ds); | ||||
transaction.Commit(); | transaction.Commit(); | ||||
cmd.Parameters.Clear(); | cmd.Parameters.Clear(); | ||||
@@ -221,7 +115,6 @@ namespace CnasSynchronusDAL | |||||
AppLog.Error(e2.Message); | AppLog.Error(e2.Message); | ||||
throw; | throw; | ||||
} | } | ||||
AppLog.Error(e1.Message); | AppLog.Error(e1.Message); | ||||
throw; | throw; | ||||
} | } | ||||
@@ -233,17 +126,10 @@ namespace CnasSynchronusDAL | |||||
AppLog.Error(e.Message); | AppLog.Error(e.Message); | ||||
throw; | throw; | ||||
} | } | ||||
finally | |||||
{ | |||||
if (connection != null) | |||||
{ | |||||
connection.Close(); | |||||
} | |||||
} | |||||
} | } | ||||
/// <summary> | /// <summary> | ||||
/// 执行sql 返回一个DataTable | |||||
/// 执行查询,返回DataTable | |||||
/// </summary> | /// </summary> | ||||
/// <param name="sqlText"></param> | /// <param name="sqlText"></param> | ||||
/// <param name="parameters"></param> | /// <param name="parameters"></param> | ||||
@@ -256,27 +142,17 @@ namespace CnasSynchronusDAL | |||||
using (DmDataAdapter adapter = new DmDataAdapter(sqlText, mConnStr)) | using (DmDataAdapter adapter = new DmDataAdapter(sqlText, mConnStr)) | ||||
{ | { | ||||
dt = new DataTable(); | dt = new DataTable(); | ||||
adapter.SelectCommand.Parameters.AddRange(parameters); | |||||
adapter.Fill(dt); | adapter.Fill(dt); | ||||
return dt; | |||||
} | } | ||||
} | } | ||||
catch (Exception ex) | catch (Exception ex) | ||||
{ | { | ||||
AppLog.Error(ex.Message); | AppLog.Error(ex.Message); | ||||
throw; | |||||
} | } | ||||
return null; | |||||
return dt; | |||||
} | } | ||||
/// <summary> | |||||
/// 准备执行一个命令 | |||||
/// </summary> | |||||
/// <param name="cmd">sql命令</param> | |||||
/// <param name="conn">数据库连接</param> | |||||
/// <param name="trans">数据库事务</param> | |||||
/// <param name="cmdType">命令类型例如 存储过程或者文本</param> | |||||
/// <param name="cmdText">命令文本,例如:Select * from Products</param> | |||||
/// <param name="cmdParms">执行命令的参数</param> | |||||
private static void PrepareCommand(DmCommand cmd, DmConnection conn, DmTransaction trans, CommandType cmdType, string cmdText, DmParameter[] cmdParms) | private static void PrepareCommand(DmCommand cmd, DmConnection conn, DmTransaction trans, CommandType cmdType, string cmdText, DmParameter[] cmdParms) | ||||
{ | { | ||||
if (conn.State != ConnectionState.Open) | if (conn.State != ConnectionState.Open) | ||||
@@ -322,5 +198,57 @@ namespace CnasSynchronusDAL | |||||
return bIfSuccess; | return bIfSuccess; | ||||
} | } | ||||
/// <summary> | |||||
/// 对达梦数据库执行增删改操作,返回受影响的行数。 | |||||
/// </summary> | |||||
/// <param name="sql">要执行的增删改的SQL语句</param> | |||||
/// <param name="parameters">SQL参数数组</param> | |||||
/// <returns>受影响的行数</returns> | |||||
public static int ExecuteNonQuery(String sql, DmParameter[] parameters) | |||||
{ | |||||
DmConnection connection = new DmConnection(mConnStr); | |||||
try | |||||
{ | |||||
using (connection) | |||||
{ | |||||
connection.Open(); | |||||
DmTransaction transaction = (DmTransaction)connection.BeginTransaction(); | |||||
using (DmCommand cmd = new DmCommand()) | |||||
{ | |||||
try | |||||
{ | |||||
PrepareCommand(cmd, connection, transaction, CommandType.Text, sql, parameters); | |||||
int rows = cmd.ExecuteNonQuery(); | |||||
transaction.Commit(); | |||||
cmd.Parameters.Clear(); | |||||
return rows; | |||||
} | |||||
catch (DmException e1) | |||||
{ | |||||
try | |||||
{ | |||||
transaction.Rollback(); | |||||
} | |||||
catch (Exception e2) | |||||
{ | |||||
AppLog.Error(e2.Message); | |||||
throw; | |||||
} | |||||
AppLog.Error(e1.Message); | |||||
throw; | |||||
} | |||||
} | |||||
} | |||||
} | |||||
catch (Exception e) | |||||
{ | |||||
AppLog.Error(e.Message); | |||||
throw; | |||||
} | |||||
} | |||||
} | } | ||||
} | } |
@@ -0,0 +1,143 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Data; | |||||
using System.Data.Entity; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
using CnasSynchronousCommon; | |||||
using CnasSynchronusIDAL; | |||||
using CnasSynchrousModel; | |||||
namespace CnasSynchronusDAL | |||||
{ | |||||
public class DmDBService : IDmDBService | |||||
{ | |||||
public bool TestConnect(string strHost, string strName, string strUser, string strPwd, string strPort) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(strHost, strPort, strName, strUser, strPwd); | |||||
return mySQLBase.LinkCnasTest(); | |||||
} | |||||
public Dictionary<string, DataTable> GetInstrumentData(DmOpenParams t) | |||||
{ | |||||
DmDAL mySQL = new DmDAL(); | |||||
mySQL.CreateConnectString(t.StrHost, t.StrPort, t.StrServer, t.StrUser, t.StrPwd); | |||||
return mySQL.GetAllTableNameAndStructure(t.StrServer); | |||||
} | |||||
public DataTable GetInstrumentDataByDate(DmOpenParams t, ConditionParams u) | |||||
{ | |||||
DmDAL mySQL = new DmDAL(); | |||||
mySQL.CreateConnectString(t.StrHost, t.StrPort, t.StrServer, t.StrUser, t.StrPwd); | |||||
return mySQL.GetDataByDateColumn( | |||||
t.StrServer, | |||||
t.autoSql.DmViewName, | |||||
t.autoSql.DmViewSql, | |||||
u.TableName, | |||||
u.DateColumn, | |||||
u.DateValue); | |||||
} | |||||
public DataTable GetInstrumentDataStruct(DmOpenParams t, ConditionParams u) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(t.StrHost, t.StrPort, t.StrServer, t.StrUser, t.StrPwd); | |||||
return mySQLBase.GetTableStruct( | |||||
u.TableName, | |||||
t.autoSql.DmViewName, | |||||
t.autoSql.DmViewSql | |||||
); | |||||
} | |||||
public DataTable GetAllCNASTablesName(DataBaseInfo dataBase) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
return mySQLBase.GetTableNames(dataBase.DBName); | |||||
} | |||||
public DataTable GetCNASTablesStruct(string strTableName, DataBaseInfo dataBase) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
return mySQLBase.GetTableStruct(strTableName,"",""); | |||||
} | |||||
public int InsertDataToCNASTable(DataTable dt, DataBaseInfo dataBase, List<SyncParamasInfo> syncParamasInfos, string strInstrumentColumn,List<CnasConditionMapValue> lstFixedValue = null) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
return mySQLBase.InsertCnasData(dt, syncParamasInfos, lstFixedValue, strInstrumentColumn); | |||||
} | |||||
public DataTable GetCNASTableTypeLenth(string strTableName, DataBaseInfo dataBase) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
return mySQLBase.GetTableTypeAndLenth(strTableName); | |||||
} | |||||
public DataTable GetLoginNameByPwd(DataBaseInfo dataBase, string strUserName, string strPwd) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
strPwd = EncryptionOperation.GetMD5Hash(strPwd); | |||||
AppLog.Info(strPwd); | |||||
//return mySQLBase.GetLoginNameByPwd(strUserName, strPwd); | |||||
return new DataTable(); | |||||
} | |||||
public bool CheckMacMessage(DataBaseInfo dataBase, string strMac) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
return mySQLBase.CheckMacMessage(strMac); | |||||
} | |||||
public bool CheckMacMessage(string strConnectionString, string strMac) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(strConnectionString); | |||||
return mySQLBase.CheckMacMessage(strMac); | |||||
} | |||||
public string GetMaxTimeByTableName(DataBaseInfo dataBase, string strTableName, string strDateColumn, string strInstrumentColumn, string strInstrumentValue) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
return mySQLBase.GetMaxTimeByTableName(strTableName, strDateColumn, strInstrumentColumn, strInstrumentValue); | |||||
} | |||||
/// <summary> | |||||
/// 获取某个表的数据 | |||||
/// </summary> | |||||
/// <param name="dataBase"></param> | |||||
/// <param name="strSql"></param> | |||||
/// <returns></returns> | |||||
public DataTable GetTableData(DataBaseInfo dataBase, string strSql) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
return mySQLBase.GetTableData(strSql); | |||||
} | |||||
/// <summary> | |||||
/// 插入某个表的数据 | |||||
/// </summary> | |||||
/// <param name="dataBase"></param> | |||||
/// <param name="strSql"></param> | |||||
/// <returns></returns> | |||||
public bool InsertTableData(DataBaseInfo dataBase, string strSql) | |||||
{ | |||||
DmDAL mySQLBase = new DmDAL(); | |||||
mySQLBase.CreateConnectString(dataBase.DBHost, dataBase.DBPort, dataBase.DBName, dataBase.DBUser, dataBase.DBPwd); | |||||
return mySQLBase.InsertTableData(strSql); | |||||
} | |||||
} | |||||
} |
@@ -23,6 +23,11 @@ namespace CnasSynchronusIDAL | |||||
} | } | ||||
public interface IDmDBService : ITargetDataBaseService, ISourceDataBaseService<DmOpenParams, ConditionParams> | |||||
{ | |||||
} | |||||
public interface IDB2DBService : ITargetDataBaseService | public interface IDB2DBService : ITargetDataBaseService | ||||
{ | { | ||||
@@ -50,6 +50,7 @@ | |||||
<Compile Include="ApiParams\LoginMessageParam.cs" /> | <Compile Include="ApiParams\LoginMessageParam.cs" /> | ||||
<Compile Include="ApiParams\TableMessageParam.cs" /> | <Compile Include="ApiParams\TableMessageParam.cs" /> | ||||
<Compile Include="AutoSql.cs" /> | <Compile Include="AutoSql.cs" /> | ||||
<Compile Include="Param\DmOpenParams.cs" /> | |||||
<Compile Include="Param\KingbaseOpenParams.cs" /> | <Compile Include="Param\KingbaseOpenParams.cs" /> | ||||
<Compile Include="Param\PostgreSQLOpenParams .cs" /> | <Compile Include="Param\PostgreSQLOpenParams .cs" /> | ||||
<Compile Include="SourceConfigFile\AccessFormatConfig.cs" /> | <Compile Include="SourceConfigFile\AccessFormatConfig.cs" /> | ||||
@@ -0,0 +1,8 @@ | |||||
namespace CnasSynchrousModel | |||||
{ | |||||
public class DmAutoSql | |||||
{ | |||||
public string DmViewName { get; set; } | |||||
public string DmViewSql { get; set; } | |||||
} | |||||
} |
@@ -0,0 +1,13 @@ | |||||
namespace CnasSynchrousModel | |||||
{ | |||||
public class DmFormatConfig | |||||
{ | |||||
public string DmFileDateColumnFormat { get; set; } | |||||
public DmAutoSql AutoSql { get; set; } | |||||
public DmFormatConfig() | |||||
{ | |||||
AutoSql = new DmAutoSql(); | |||||
} | |||||
} | |||||
} |
@@ -0,0 +1,12 @@ | |||||
namespace CnasSynchrousModel | |||||
{ | |||||
public class DmOpenParams | |||||
{ | |||||
public string StrHost { get; set; } | |||||
public string StrServer { get; set; } | |||||
public string StrUser { get; set; } | |||||
public string StrPwd { get; set; } | |||||
public string StrPort { get; set; } | |||||
public DmAutoSql autoSql { get; set; } | |||||
} | |||||
} |
@@ -0,0 +1,31 @@ | |||||
using System; | |||||
using System.Collections.Generic; | |||||
using System.Linq; | |||||
using System.Text; | |||||
using System.Threading.Tasks; | |||||
namespace CnasSynchrousModel | |||||
{ | |||||
public class DmOpenParams : OpenSourceParams | |||||
{ | |||||
/// <summary> | |||||
/// 服务器IP | |||||
/// </summary> | |||||
public string StrHost { get; set; } | |||||
/// <summary> | |||||
/// 服务器实例名 | |||||
/// </summary> | |||||
public string StrServer { get; set; } | |||||
/// <summary> | |||||
/// 端口 | |||||
/// </summary> | |||||
public string StrPort { get; set; } | |||||
/// <summary> | |||||
/// 自定义查询 | |||||
/// </summary> | |||||
public DmAutoSql autoSql { get; set; } | |||||
} | |||||
} |