/// <summary> /// 从数据库得到来源数据 /// </summary> /// <returns></returns> public DataTable GetSourceDataFromDB(Hashtable parameters, TableMap tableMap) { DataTable dtSource = null; DataAccessBroker brokerSource = DataAccessFactory.Instance(this.SourceDataAccessCfg); try { string sqlString = tableMap.GetSourceSelectSQL(this.MaxCount); // 替换sql中的宏变量 sqlString = BaseCommand.ReplaceParameters(this.InitialParameters, sqlString); DataSet dataSetSource = brokerSource.FillSQLDataSet(sqlString); // 源数据为空时,不做任何操作 if (dataSetSource == null || dataSetSource.Tables.Count == 0) { return(null); } // 取第一个表 dtSource = dataSetSource.Tables[0]; } finally { brokerSource.Close(); } // 写日志 LogManager.Current.WriteCommonLog(this.JobCode, "从数据表" + tableMap.SourceTable.TableName + "获得" + dtSource.Rows.Count.ToString() + "笔记录。", this.ThreadName); return(dtSource); }
public string TargetDir = ""; // 目标目录 #region ICommand Members /* * <Zip Type="Compress" * SourceFileName="%FileName%.txt" * TargetFileName="%FileName%.zip"/> */ /// <summary> /// 初始化 /// </summary> /// <param name="appDir"></param> /// <param name="node"></param> public override void Initialize(Hashtable parameters, XmlNode node) { // 先调基类函数 base.Initialize(parameters, node); // 解析参数 this.Type = XmlUtil.GetAttrValue(node, "Type"); this.SourceFileName = XmlUtil.GetAttrValue(node, "SourceFileName"); this.SourceFileName = BaseCommand.ReplaceParameters(parameters, this.SourceFileName); this.TargetFileName = XmlUtil.GetAttrValue(node, "TargetFileName"); this.TargetFileName = BaseCommand.ReplaceParameters(parameters, this.TargetFileName); }
/// <summary> /// 从文件得到detail /// </summary> /// <param name="tableMap"></param> /// <param name="dtHeader"></param> /// <returns></returns> public DataTable GetDetailDataFromFile(Hashtable parameters, TableMap tableMap, DataTable dtHeader) { string fileName = BaseCommand.ReplaceParameters(parameters, tableMap.SourceTable.FileName); DataTable dtDetail = new DataTable();; if (dtHeader == null) { return(null); } string[] refFieldList = tableMap.SourceTable.RefFields.Split(new char[] { ',' }); // 拼出关联条件 List <string> refDataIDList = GetDataIDList(dtHeader, tableMap.SourceTable.RefFields, DDPConst.C_ValueSplitOperator); if (refDataIDList.Count == 0) // 没有对应的关联数据 { return(null); } // 创建表结构 string[] fields = tableMap.SourceTable.FieldNames.Split(new char[] { ',' }); for (int i = 0; i < fields.Length; i++) { string field = fields[i].Trim(); DataColumn col = new DataColumn(field); dtDetail.Columns.Add(col); } // 读取文件中的数据 StreamReader sr = new StreamReader(fileName, Encoding.Default); //this.JobEntity.TableMap.SourceFile try { int nCount = 0; while (sr.EndOfStream == false) { Application.DoEvents(); // 读取一行 string line = sr.ReadLine().Trim(); if (line == "") { continue; } // 按分隔符拆分字段 string[] fieldValues = line.Split(new char[] { tableMap.SourceTable.FieldSplitOperator }); // 创建行,并给各字段赋值 DataRow row = dtDetail.NewRow(); for (int i = 0; i < fieldValues.Length && i < dtDetail.Columns.Count; i++) { string fieldName = fields[i].Trim(); string fieldValue = fieldValues[i]; row[fieldName] = fieldValue; } // 支持多个主键 string keyValues = ""; for (int x = 0; x < refFieldList.Length; x++) { string key = refFieldList[x]; string value = row[key].ToString(); if (keyValues != "") { keyValues += DDPConst.C_ValueSplitOperator; } keyValues += value; } if (refDataIDList.IndexOf(keyValues) == -1) { LogManager.Current.WriteCommonLog(this.JobCode, keyValues + "未找到匹配的的header记录。", this.ThreadName); } else { dtDetail.Rows.Add(row); } } } finally { sr.Close(); } return(dtDetail); }
/// <summary> /// 执行 /// </summary> /// <param name="parameters"></param> /// <param name="error"></param> /// <returns></returns> public override ResultCode Execute(ref Hashtable parameters, out string error) { error = ""; // 先将文件名中的参数替换掉 string targetFileName = BaseCommand.ReplaceParameters(this.InitialParameters, this._tableMap.TargetTable.FileName); // 确保目录存在 int nIndex = targetFileName.LastIndexOf("\\"); if (nIndex != -1) { string dir = targetFileName.Substring(0, nIndex); if (Directory.Exists(dir) == false) { Directory.CreateDirectory(dir); } } int count = 0; DataAccessBroker brokerSource = DataAccessFactory.Instance(this.SourceDataAccessCfg); // 将数据保存到目标文件 StreamWriter sw = null; if (String.Compare(this._tableMap.TargetTable.Encoding, "UTF8", true) == 0) { sw = new StreamWriter(targetFileName, false, Encoding.UTF8); //C2-CI指定utf8 } else { sw = new StreamWriter(targetFileName, false); } try { string sqlString = ""; sw.BaseStream.Seek(0, SeekOrigin.Begin); // 判断是否第一行输出总记录数 if (this._tableMap.TargetTable.OneRowRecordsNum == true) { string header = "#"; header = header.PadRight(10, '#'); sw.WriteLine(header); } // 依次 sqlString = this._tableMap.GetSourceSelectSQL(this.MaxCount); IDataReader dataReader = brokerSource.ExecuteSQLReader(sqlString); while (dataReader.Read()) { string line = ""; for (int i = 0; i < dataReader.FieldCount; i++) { if (line != "") { line += this._tableMap.TargetTable.FieldSplitOperator; } line += dataReader[i].ToString(); } sw.WriteLine(line); count++; } sw.Flush(); // 判断是否第一行输出总记录数 if (this._tableMap.TargetTable.OneRowRecordsNum == true) { sw.BaseStream.Seek(0, SeekOrigin.Begin); string header = "#" + count.ToString(); sw.WriteLine(header.PadRight(10), ' '); sw.Flush(); } } finally { if (sw != null) { sw.Flush(); sw.Close(); } brokerSource.Close(); } // 写日志 LogManager.Current.WriteCommonLog(this.JobCode, "从数据表" + this._tableMap.SourceTable.TableName + "获得" + count.ToString() + "笔记录。", this.ThreadName); return(ResultCode.Success); }
/// <summary> /// 保存到目标文件 /// </summary> /// <param name="dtSource"></param> public void SaveToTargetFile(TableMap tableMap, DataTable dtSource, bool bMaxCount) { // 先将文件名中的参数替换掉 string targetFileName = BaseCommand.ReplaceParameters(this.InitialParameters, tableMap.TargetTable.FileName); // 确保目录存在 int nIndex = targetFileName.LastIndexOf("\\"); if (nIndex != -1) { string dir = targetFileName.Substring(0, nIndex); if (Directory.Exists(dir) == false) { Directory.CreateDirectory(dir); } } // 将数据保存到目标文件 StreamWriter sw = null; if (String.Compare(tableMap.TargetTable.Encoding, "UTF8", true) == 0) { sw = new StreamWriter(targetFileName, false, Encoding.UTF8); //C2-CI指定utf8 } else { sw = new StreamWriter(targetFileName, false); } try { // 最大记录数限制 int nCount = dtSource.Rows.Count; if (bMaxCount == true) { if (this.MaxCount != -1 && nCount > this.MaxCount) { nCount = this.MaxCount; } } // 判断是否第一行输入行数 if (tableMap.TargetTable.OneRowRecordsNum == true) { sw.WriteLine("#" + nCount.ToString()); } int addFieldCount = 0; if (tableMap.SourceTable.PrimaryKeys != "") { addFieldCount = tableMap.SourceTable.PrimaryKeyList.Length; } // 导出每一行 for (int i = 0; i < nCount; i++) { DataRow row = dtSource.Rows[i]; string line = ""; for (int j = 0; j < row.Table.Columns.Count - addFieldCount; j++) //注意要减去自已加的主键列 { if (line != "") { line += tableMap.TargetTable.FieldSplitOperator; } line += row[j].ToString(); } sw.WriteLine(line); } } finally { sw.Close(); } // 写日志 LogManager.Current.WriteCommonLog(this.JobCode, "成功将数据保存到目标文件" + targetFileName, this.ThreadName); }
/// <summary> /// 从文件中得到来源数据 /// </summary> /// <param name="tableMap"></param> /// <returns></returns> public static DataTable GetSourceDataFromFile(Hashtable parameters, TableMap tableMap, bool bIgnoreInsert, string strIgnoreFields, int iStartLine) { List <string> IgnoreFields = new List <string>(); if (bIgnoreInsert && !string.IsNullOrEmpty(strIgnoreFields)) { string[] IgnoreFieldNameArray = strIgnoreFields.Split(new char[] { ',' }); for (int i = 0; i < IgnoreFieldNameArray.Length; i++) { string field = IgnoreFieldNameArray[i].Trim(); if (field != "") { IgnoreFields.Add(field); } } } string fileName = BaseCommand.ReplaceParameters(parameters, tableMap.SourceTable.FileName); // 创建表结构 string[] fields = tableMap.SourceTable.FieldNames.Split(new char[] { ',' }); DataTable dtSource = new DataTable(); for (int i = 0; i < fields.Length; i++) { string field = fields[i].Trim(); DataColumn col = new DataColumn(field); dtSource.Columns.Add(col); } if (tableMap.TargetTable.GUIDPrimaryKey != "") { DataColumn col = new DataColumn(tableMap.TargetTable.GUIDPrimaryKey); dtSource.Columns.Add(col); } int iLine = 0; // 读取文件中的数据 StreamReader sr = new StreamReader(fileName, Encoding.UTF8);//Default); //this.JobEntity.TableMap.SourceFile try { while (sr.EndOfStream == false) { Application.DoEvents(); iLine += 1; // 读取一行 string line = sr.ReadLine().Trim(); if (iLine == iStartLine) { continue; } if (line == "") { continue; } // 按分隔符拆分字段 string[] fieldValues = line.Split(new char[] { tableMap.SourceTable.FieldSplitOperator }); if (bIgnoreInsert) { if ((fields.Length + IgnoreFields.Count) != fieldValues.Length) { LogManager.Current.WriteCommonLog("获取数据", fieldValues.ToString() + "配置的字段数量'" + fields.Length.ToString() + "'个与数据中的字段数量'" + fieldValues.Length.ToString() + "'个不一致", Guid.NewGuid().ToString()); continue; } } else { if (fields.Length != fieldValues.Length) { LogManager.Current.WriteCommonLog("获取数据", fieldValues.ToString() + "配置的字段数量'" + fields.Length.ToString() + "'个与数据中的字段数量'" + fieldValues.Length.ToString() + "'个不一致", Guid.NewGuid().ToString()); continue; } } // 创建行,并给各字段赋值 DataRow row = dtSource.NewRow(); int Ignore = 0; for (int i = 0; i < fieldValues.Length; i++) { bool bIgnore = false; if (bIgnoreInsert) { foreach (string strIgnore in IgnoreFields) { if (strIgnore.Equals(i.ToString())) { bIgnore = true; Ignore += 1; break; } } } if (!bIgnore) { string fieldName = fields[i - Ignore].Trim(); string fieldValue = fieldValues[i]; if (tableMap.SourceTable.UTCTimeFields.IndexOf(fieldName) != -1) { DateTime leftDate = DateTime.Parse(fieldValue); fieldValue = leftDate.ToString("yyyy-MM-dd HH:mm:ss"); } if (tableMap.SourceTable.IntFields.IndexOf(fieldName) != -1) { fieldValue = ((int)decimal.Parse(fieldValue)).ToString(); } if (tableMap.SourceTable.ArticleFields.IndexOf(fieldName) != -1) { string[] strArticleNos = fieldValue.Split('-'); foreach (string strArticle in strArticleNos) { if (strArticle.Trim().Length == 6) { fieldValue = strArticle; } } } row[fieldName] = fieldValue; } } if (tableMap.TargetTable.GUIDPrimaryKey != "") { row[tableMap.TargetTable.GUIDPrimaryKey] = Guid.NewGuid().ToString(); } dtSource.Rows.Add(row); } } finally { sr.Close(); } // 写日志 //LogManager.Current.WriteCommonLog(jobCode, "从文件" + fileName + "获得" + dtSource.Rows.Count.ToString() + "笔记录。", threadName); return(dtSource); }
/// <summary> /// 执行 /// </summary> /// <param name="parameters"></param> /// <param name="error"></param> /// <returns></returns> public override ResultCode Execute(ref Hashtable parameters, out string error) { error = ""; bool hasDataIDList = false; if (parameters.ContainsKey(DDPConst.Param_HasDataIDList) == true) { hasDataIDList = (bool)parameters[DDPConst.Param_HasDataIDList]; } if (hasDataIDList == true) { TableMap tableMap = this.TableMapList[0];// 应该只有一个表 // 获取来源数据 List <string> dataIDList = (List <string>)parameters[DDPConst.Param_DataIDList]; // 写日志 LogManager.Current.WriteCommonLog(this.JobCode, "程序传入来源数据,记录数为" + dataIDList.Count.ToString() + "", this.ThreadName); string splitOperator = (string)parameters[DDPConst.Param_SplitOperator]; DataTable dtSource = this.GetSourceByIDList(tableMap, tableMap.SourceTable.PrimaryKeys, dataIDList, splitOperator); // 写日志 LogManager.Current.WriteCommonLog(this.JobCode, "根据ID获得的实际记录数为" + dtSource.Rows.Count.ToString(), this.ThreadName); // 无数据时不再继续 if (dtSource.Rows.Count == 0) { return(ResultCode.Break); } // 加入参数里,传给后面的命令 parameters[DDPConst.Param_PrimaryKeys] = tableMap.SourceTable.PrimaryKeys; parameters[DDPConst.Param_DataAccessCfg] = this.SourceDataAccessCfg; // 保存到目标 this.SaveToTarget(tableMap, dtSource, true); } else { for (int i = 0; i < TableMapList.Count; i++) { TableMap tableMap = TableMapList[i]; tableMap.SourceTable.FileName = BaseCommand.ReplaceParameters(parameters, tableMap.SourceTable.FileName); // 获取来源数据 DataTable dtSource = this.GetSourceData(parameters, tableMap); // 无数据时不再继续 if (dtSource.Rows.Count == 0) { return(ResultCode.Break); } // 只取第1个表的主键值,存放到list if (i == 0 && tableMap.SourceTable.PrimaryKeyList != null && dtSource != null) { List <string> dataIDList = GetDataIDList(dtSource, tableMap.SourceTable.PrimaryKeys, DDPConst.C_ValueSplitOperator); // 加入参数里,传给后面的命令 parameters[DDPConst.Param_PrimaryKeys] = tableMap.SourceTable.PrimaryKeys; parameters[DDPConst.Param_DataIDList] = dataIDList; parameters[DDPConst.Param_DataAccessCfg] = this.SourceDataAccessCfg; } // 保存到目标 this.SaveToTarget(tableMap, dtSource, true); } } return(ResultCode.Success); }