コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <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);
        }
コード例 #3
0
        /// <summary>
        /// 根据Data ID List取数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetSourceByIDList(TableMap tableMap,
                                           string fieldNames,
                                           List <string> dataIDList,
                                           string dataSplitOperator)
        {
            DataTable retTable = new DataTable();

            DataAccessBroker brokerSource = DataAccessFactory.Instance(this.SourceDataAccessCfg);

            try
            {
                for (int x = 0; x < dataIDList.Count;)
                {
                    // 每批50个
                    int count = 50;
                    if (x + count > dataIDList.Count)
                    {
                        count = dataIDList.Count - x;
                    }

                    // 多条数据以or拼起来
                    List <string> tempDataIDList = new List <string>();
                    for (int y = 0; y < count; y++)
                    {
                        tempDataIDList.Add(dataIDList[x + y]);
                    }
                    x += count;

                    // 得到where语句
                    string whereSql = BaseCommand.MakeDataIDWhereSql(fieldNames, tempDataIDList, dataSplitOperator);

                    string sql = "select " + tableMap.SourceTable.FieldNames
                                 + " from " + tableMap.SourceTable.TableName
                                 + whereSql;

                    DataSet dataSet = brokerSource.FillSQLDataSet(sql);
                    if (dataSet == null || dataSet.Tables.Count == 0)
                    {
                        continue;// 继续下一批50
                    }
                    DataTable dtTemp = dataSet.Tables[0];

                    // 第一次时初始化列
                    if (retTable.Columns.Count == 0)
                    {
                        for (int x1 = 0; x1 < dtTemp.Columns.Count; x1++)
                        {
                            retTable.Columns.Add(dtTemp.Columns[x1].ColumnName, dtTemp.Columns[x1].DataType);
                        }
                    }

                    // 将临时表的数据复制到返回的表中
                    for (int i = 0; i < dtTemp.Rows.Count; i++)
                    {
                        DataRow row   = retTable.NewRow();
                        DataRow dtRow = dtTemp.Rows[i];
                        for (int j = 0; j < retTable.Columns.Count; j++)
                        {
                            row[j] = dtRow[j];
                        }
                        retTable.Rows.Add(row);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                brokerSource.Close();
            }

            return(retTable);
        }
コード例 #4
0
        /// <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);
        }