/// <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); }