【问题标题】:Delete Rows Start in CSV file In SSIS Controlflow Script Task在 SSIS 控制流脚本任务中的 CSV 文件中删除行开始
【发布时间】:2014-03-12 09:07:36
【问题描述】:

我有一个如下所示的.csv 文件:

#Example Company                        
#(999) 999-9999                      
#http://yourwebsite.com                             
#Report Date Range: Dec 26, 2013 - Dec 26, 2013                     
#Exported: Dec 26, 2013                             
#Twitter : Profile Summary                              
#Screen Name,Name,Description,Location,Followers,Following,Listed

SctaSa,statisticalgraph,statistical Screen- The official account for your 
organization,Saudi Arabia,6775,8,75

所以,我需要从 .csv 文件中获取特定数据以供 SSIS 转换读取,从以 # 开头的列 "Screen Name"remove the garbage data 开始,看起来像这样

Screen Name,Name,Description,Location,Followers,Following,Listed,Exported,Report Date Range
SctaSa,statisticalgraph,statistical Screen- The official account for your organization,Saudi Arabia,6775,8,75,26-Dec-13,26-Dec-13

我尝试使用这个 C# 脚本,但它不包含文件 (我不是 C# 专家,所以我不知道问题是什么) 我尝试使用以下脚本删除任何以# but the file dose not transfare to the out put path 开头的行;你能给我一些建议吗?!

#region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.IO;
using System.Collections.Generic;
#endregion

namespace ST_a7b941606e0b40aa920bfe13fc81dc81
{
    /// <summary>
    /// ScriptMain is the entry point class of the script.  Do not change the name, attributes,
    /// or parent of this class.
    /// </summary>
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var lines = new List<string>();
            string line;
            using (var file = new System.IO.StreamReader("D:\\try.csv")) 
            {
                while ((line = file.ReadLine()) != null)
                {
                    if (line.Length != 0)
                    {
                        if (!line.StartsWith("#")  )
                        {
                            lines.Add(line);
                        }
                    }
                } 
           }
           File.WriteAllLines("D:\\SCTA_ETL\\try.csv", lines);
        }

        /// <summary>
        /// This method is called when this script task executes in the control flow.
        /// Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
        /// To open Help, press F1.
        /// </summary>
        public void Main()
        {
            // TODO: Add your code here

            Dts.TaskResult = (int)ScriptResults.Success;
        }

        #region ScriptResults declaration
        /// <summary>
        /// This enum provides a convenient shorthand within the scope of this class for setting the
        /// result of the script.
        /// 
        /// This code was generated automatically.
        /// </summary>
        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion

    }
}

【问题讨论】:

  • 请不要在问题标题中包含有关所用语言的信息,除非没有它就没有意义。标记用于此目的。

标签: c# parsing csv ssis


【解决方案1】:

另一种方式:

File.WriteAllLines(outputPath, File.ReadAllLines("c:\\mycsv.csv").Where(x => !x.StartsWith("#")).ToArray());

【讨论】:

  • 拜托,你能把整个脚本写给我直接执行吗? :)
  • 我的代码在 base.PreExecute(); 之后替换了你所有的脚本;
  • 你可以包装代码来尝试/捕获块来处理异常。
【解决方案2】:

你可能想在中间改变你的逻辑:

var lines = new List<string>();
string outputPath = // your output path here
using (var file = new System.IO.StreamReader("c:\\mycsv.csv")) 
{
  string line;
  while ((line = file.ReadLine()) != null)
  {
    if (!line.StartsWith("#"))
    {
      lines.Add(line);
    }
  } 
}
File.WriteAllLines(outputPath, lines);

您一直在删除所有带有“#”的行。

相反,只添加不以“#”开头的行。

另外,请务必在完成后关闭并处理您的 StreamReader,或者将整个内容放入 using 部分。

【讨论】:

  • 拜托,你能把整个脚本写给我直接执行吗? :)
  • 我添加了完整的脚本
  • 我使用了您的脚本,但它不会将文件转换为输出路径,
  • 我刚刚在控制台应用程序中测试了这个确切的代码(只有Main() 中的这些行,唯一的区别是我定义了outputPath 并在if 行上添加了缺少的右括号)它工作得很好。你得到了什么例外?
  • 我在 SSIS 控制流“脚本任务”中使用此脚本,但它不会将新文件移动到输出路径
猜你喜欢
  • 2015-11-22
  • 2015-05-03
  • 2023-03-21
  • 1970-01-01
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
  • 2020-12-15
  • 2014-07-16
相关资源
最近更新 更多