Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ';C“在加载.CSV文件之前删除四个引号的脚本任务”;_C#_Ssis_Etl_Flat File_Script Task - Fatal编程技术网

C# ';C“在加载.CSV文件之前删除四个引号的脚本任务”;

C# ';C“在加载.CSV文件之前删除四个引号的脚本任务”;,c#,ssis,etl,flat-file,script-task,C#,Ssis,Etl,Flat File,Script Task,我有一个相当基本的SSIS包,可以将.csv文件加载到SQL表中。但是,当包尝试读取数据流任务中的.csv源时,我收到错误消息:“未找到列“X”的列分隔符。处理数据行“Y”上的文件“file.csv”时出错。” 在本例中,发生的情况是数千行中有几行包含四个引号内的字符串,即“Jane”Jill“Doe”。在UltraEdit works中手动删除这些行中的引号,但是,我正在尝试自动化这些包。派生列不起作用,因为它与分隔符有关 事实证明,在包可以正确加载文件之前,我需要一个脚本任务来删除四个引号。

我有一个相当基本的SSIS包,可以将.csv文件加载到SQL表中。但是,当包尝试读取数据流任务中的.csv源时,我收到错误消息:“未找到列“X”的列分隔符。处理数据行“Y”上的文件“file.csv”时出错。”

在本例中,发生的情况是数千行中有几行包含四个引号内的字符串,即“Jane”Jill“Doe”。在UltraEdit works中手动删除这些行中的引号,但是,我正在尝试自动化这些包。派生列不起作用,因为它与分隔符有关

事实证明,在包可以正确加载文件之前,我需要一个脚本任务来删除四个引号。以下代码(我从各种来源拼凑而成)被SSIS认为是无错误的,但在执行时遇到DTS脚本任务运行时错误:

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

namespace ST_a881d570d1a6495e84824a72bd28f44f
 {
[Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
{
    public void Main()
    {
        // TODO: Add your code here
        var fileContents = System.IO.File.ReadAllText(@"C:\\File.csv");

        fileContents = fileContents.Replace("<body>", "<body onload='jsFx();' />");
        fileContents = fileContents.Replace("</body>", "</body>");

        System.IO.File.WriteAllText(@"C:\\File.csv", fileContents);

    }

    #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#示例将搜索csv文件,删除包含双引号文本的任何双引号,然后将修改后的内容写回该文件。正则表达式返回任何双引号上的匹配项,该双引号不在字符串的开头或结尾,或者前后没有逗号,并用空字符串替换双引号。您可能已经在执行此操作,但请确保保存文件路径的变量列在脚本任务的
ReadOnlyVariables
字段中

using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;


string filePath = Dts.Variables["User::FilePath"].Value.ToString();

List<String> outputRecords = new List<String>();
if (File.Exists(filePath))
{
 using (StreamReader rdr = new StreamReader(filePath))
 {
  string line;
  while ((line = rdr.ReadLine()) != null)
  {
      if (line.Contains(","))
      {
          string[] split = line.Split(',');

       //replace double qoutes between text
       line = Regex.Replace(line, "(?<!(,|^))\"(?!($|,))", x => x.Value.Replace("\"", ""));

      }
      outputRecords.Add(line);
    }
 }

 using (StreamWriter sw = new StreamWriter(filePath, false))
 {
     //write filtered records back to file
     foreach (string s in outputRecords)
         sw.WriteLine(s);
  }
}
使用System.Collections.Generic;
使用System.IO;
使用System.Text.RegularExpressions;
字符串filePath=Dts.Variables[“User::filePath”].Value.ToString();
List outputRecords=新列表();
if(File.Exists(filePath))
{
使用(StreamReader rdr=新的StreamReader(文件路径))
{
弦线;
而((line=rdr.ReadLine())!=null)
{
if(第行包含(“,”)
{
string[]split=line.split(',');
//替换文本之间的双qoutes

line=Regex.Replace(line,“(?当我看到这个问题与c有关时,我正等着你回答:)谢谢@Hadi:),在经历了足够多的奇怪场景之后,我已经习惯于使用C#来查找类似的内容。CSV文件可以在字段中包含双引号,只要它们被转义就可以。SSIS平面文件源不支持这一点。SQL Server 2017为添加了完整的CSV支持。您可以使用它,而不是修改应该使用的源文件。无论哪个客户端执行该命令。该路径应该是可访问的,即它应该是服务器本身的路径或SQL server服务帐户可见的网络共享。我显然没有运行批量插入的权限级别。感谢您的建议!
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;


string filePath = Dts.Variables["User::FilePath"].Value.ToString();

List<String> outputRecords = new List<String>();
if (File.Exists(filePath))
{
 using (StreamReader rdr = new StreamReader(filePath))
 {
  string line;
  while ((line = rdr.ReadLine()) != null)
  {
      if (line.Contains(","))
      {
          string[] split = line.Split(',');

       //replace double qoutes between text
       line = Regex.Replace(line, "(?<!(,|^))\"(?!($|,))", x => x.Value.Replace("\"", ""));

      }
      outputRecords.Add(line);
    }
 }

 using (StreamWriter sw = new StreamWriter(filePath, false))
 {
     //write filtered records back to file
     foreach (string s in outputRecords)
         sw.WriteLine(s);
  }
}