Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.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
System.Text.RegularExpressions.Regex.Replace C#中的SSI错误_C#_Regex_Ssis - Fatal编程技术网

System.Text.RegularExpressions.Regex.Replace C#中的SSI错误

System.Text.RegularExpressions.Regex.Replace C#中的SSI错误,c#,regex,ssis,C#,Regex,Ssis,我使用下面的代码在C#中编写ssis包,当我编写此代码时,我得到一个错误 using System; using System.Data; using Microsoft.SqlServer.Dts.Pipeline.Wrapper; using Microsoft.SqlServer.Dts.Runtime.Wrapper; using System.Text.RegularExpressions; [Microsoft.SqlServer.D

我使用下面的代码在C#中编写ssis包,当我编写此代码时,我得到一个错误

    using System;
    using System.Data;
    using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
    using Microsoft.SqlServer.Dts.Runtime.Wrapper;
    using System.Text.RegularExpressions;

    [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
    public class ScriptMain : UserComponent
    {

        public override void PreExecute()
        {
            base.PreExecute();
        }
        public override void PostExecute()
        {
            base.PostExecute();
        }
        string toreplace = "[~!@#$%^&*()_+`{};':,./<>?]";
        string replacewith = "";
        public override void Input0_ProcessInputRow(Input0Buffer Row)
        {
            Regex reg = new Regex(toreplace);
            Row.NaN = reg.Replace(Row.Na, replacewith);


        }

    }
这里,
Na
是输入列,
NaN
是输出列,两者都是在input列中带有特殊字符的varchar

例外情况:

System.ArgumentNullException
System.ArgumentOutofRangeException
这是SSIS包中BufferWrapper中的代码

/* THIS IS AUTO-GENERATED CODE THAT WILL BE OVERWRITTEN! DO NOT EDIT!
*  Microsoft SQL Server Integration Services buffer wrappers
*  This module defines classes for accessing data flow buffers
*  THIS IS AUTO-GENERATED CODE THAT WILL BE OVERWRITTEN! DO NOT EDIT! */



    using System;
    using System.Data;
    using Microsoft.SqlServer.Dts.Pipeline;
    using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

    public class Input0Buffer: ScriptBuffer

    {
        public Input0Buffer(PipelineBuffer Buffer, int[] BufferColumnIndexes, OutputNameMap OutputMap)
            : base(Buffer, BufferColumnIndexes, OutputMap)
        {
        }

        public BlobColumn Na
        {
            get
            {
                return (BlobColumn)Buffer[BufferColumnIndexes[0]];
            }
        }
        public bool Na_IsNull
        {
            get
            {
                return IsNull(0);
            }
        }

        public Int32 NaN
        {
            set
            {
                this[1] = value;
            }
        }
        public bool NaN_IsNull
        {
            set
            {
                if (value)
                {
                    SetNull(1);
                }
                else
                {
                    throw new InvalidOperationException("IsNull property cannot be set to False. Assign a value to the column instead.");
                }
            }
        }

        new public bool NextRow()
        {
            return base.NextRow();
        }

        new public bool EndOfRowset()
        {
            return base.EndOfRowset();
        }

    }
数据流

脚本组件,输入列

脚本组件,实际脚本


您的代码基本上很好。您没有测试
Na
列是否为空。也许您的源数据不允许空值,因此不需要测试

您可以通过在成员级别确定正则表达式的范围并在预执行方法中实例化它来提高性能,但这只是性能问题。与您收到的错误消息无关

你可以看到我的包和预期的结果。我向下发送了4行,一行为空值,一行不应更改,两行需要更改

我的数据流 我已经更新了我的数据流,以匹配您在变色龙问题中使用的步骤

我的源代码查询 我生成了2列数据和4行数据。与原始问题匹配的Na列是varchar类型。列“机构名称”将强制转换为不推荐使用的文本数据类型,以匹配后续更新

SELECT 
    D.Na
,   CAST(D.Na AS text) AS Agency_Names
FROM
(
SELECT 'Hello world' AS Na
UNION ALL SELECT 'man~ana'
UNION ALL SELECT 'p@$$word!'
UNION ALL SELECT NULL
) D (Na);
数据转换 我在OLE DB源代码之后添加了数据转换。为了反映您所做的工作,我将我的
机构名称
转换为长度为50的
字符串[DT_STR]
数据类型,并将其别名为“机构名称副本”

元数据 此时,我将验证数据流的元数据是否为DT_STR或DT_WSTR类型,这是即将调用正则表达式时唯一允许的输入。我确认,
机构名称副本
是预期的数据类型

脚本任务 我为
Na
Copy of Agency_Name
列指定了只读用法,并将后者别名为“AgencyNames”

我添加了两个输出列:NaN,它与您的原始问题相匹配,并创建了AgencyNamesCleaned。它们都配置为DT_STR,代码页1252,长度为50

这是我使用的脚本

public class ScriptMain : UserComponent
{

    string toreplace = "[~!@#$%^&*()_+`{};':,./<>?]";
    string replacewith = "";


    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        Regex reg = new Regex(toreplace);

        // Test for nulls otherwise Replace will blow up
        if (!Row.Na_IsNull)
        {
            Row.NaN = reg.Replace(Row.Na, replacewith);
        }
        else
        {
            Row.NaN_IsNull = true;
        }

        if (!Row.AgencyNames_IsNull)
        {
            Row.AgencyNamesCleaned = reg.Replace(Row.AgencyNames, replacewith);
        }
        else
        {
            Row.AgencyNamesCleaned_IsNull = true;
        }
    }

}
源系统提供了元数据,SSIS认为此列是二进制数据。可能是主机中的NTEXT/TEXT或n/varchar(max)。您需要做一些事情,使其成为正则表达式的兼容操作数。我会清理源代码中的列类型,但如果这不是一个选项,则使用
数据转换
转换将其转换为DT_STR/DT_WSTR类型

结局 您可以在附在我的第一张图像上的Data Viewer中观察到,NaN和AgencyNamesCleaned已正确删除了有问题的字符。此外,您可以观察到,我的脚本任务没有像您的脚本任务那样附加红色X。这表示脚本处于无效状态

由于您已经从数据转换组件创建了“机构名称副本”列作为DT_文本,将其连接到脚本组件,然后更改了数据转换组件中的数据类型,因此可以通过让转换刷新其元数据来解析脚本上的红色X。打开脚本并单击“重新编译”(ctrl-shift-b)以获得良好的度量


reg.Replace中不应有下划线(…
code。如果有的话,你的问题还有另一个方面没有得到沟通。在这一点上,我最好的建议是重新创建一个概念验证包,正如我所描述的那样,如果它起作用,它将成为一个练习,找出你有工作和没有工作之间的区别。

Yo您的代码基本上没有问题。您没有测试
Na
列是否为空。可能您的源数据不允许为空,因此无需测试

您可以通过在成员级别确定正则表达式的范围并在预执行方法中实例化它来提高性能,但这只是性能问题。与您收到的错误消息无关

您可以看到我的包和预期结果。我向下发送了4行,一行为空值,一行不应更改,两行需要更改

我的数据流 我已经更新了我的数据流,以匹配您在变色龙问题中使用的步骤

我的源代码查询 我生成了2列数据和4行值。与原始问题匹配的Na列的类型为varchar。列名称转换为不推荐的文本数据类型,以匹配后续更新

SELECT 
    D.Na
,   CAST(D.Na AS text) AS Agency_Names
FROM
(
SELECT 'Hello world' AS Na
UNION ALL SELECT 'man~ana'
UNION ALL SELECT 'p@$$word!'
UNION ALL SELECT NULL
) D (Na);
数据转换 我在OLE DB源代码之后添加了一个数据转换。为了反映您所做的工作,我将我的
机构名称
转换为长度为50的
字符串[DT_STR]
数据类型,并将其别名为“机构名称副本”

元数据 在这一点上,我验证了我的数据流的元数据是DT_STR或DT_WSTR类型,这是即将调用正则表达式的唯一允许的输入。我确认
代理名称的副本
是预期的数据类型

脚本任务 我为
Na
Copy of Agency_Name
列指定了只读用法,并将后者别名为“AgencyNames”

我添加了两个输出列:NaN,它与您的原始问题相匹配,并创建了AgencyNamesCleaned。这两个列都配置为DT_STR,代码页1252,长度为50

这是我使用的脚本

public class ScriptMain : UserComponent
{

    string toreplace = "[~!@#$%^&*()_+`{};':,./<>?]";
    string replacewith = "";


    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        Regex reg = new Regex(toreplace);

        // Test for nulls otherwise Replace will blow up
        if (!Row.Na_IsNull)
        {
            Row.NaN = reg.Replace(Row.Na, replacewith);
        }
        else
        {
            Row.NaN_IsNull = true;
        }

        if (!Row.AgencyNames_IsNull)
        {
            Row.AgencyNamesCleaned = reg.Replace(Row.AgencyNames, replacewith);
        }
        else
        {
            Row.AgencyNamesCleaned_IsNull = true;
        }
    }

}
您的源系统提供了元数据,因此SSIS认为此列是二进制数据。可能是主机中的NTEXT/TEXT或n/varchar(max)。您需要采取措施使其成为正则表达式的兼容操作数。我将清理列