【问题标题】:How to append pipe character in a comma delimited string?如何在逗号分隔的字符串中附加管道字符?
【发布时间】:2021-10-10 20:12:39
【问题描述】:

我有一个 SSIS 包,它从 SQL 数据库中提取数据并生成逗号分隔的平面文件。平面文件用于将数据导入系统,它会导致文件中的文本字段值出现问题,因为它在值中包含逗号。

我现在被告知插入管道字符 |作为所有文本字段的文本限定符。

示例 1234,Smith, John,5678 应该是 1234,|Smith, John|,5678

我按照link 中的教程创建了用于导出的 SSIS 包。它正在使用脚本任务和粘贴在下面的 Visual C# 脚本,我发现很难修改如何将管道字符添加到由逗号字符组成的文本值之前。

我认为这是我需要插入管道字符的部分,但我对 C# 语言的了解不足以根据需要对其进行修改。任何帮助或参考资源都会非常有帮助和感激。

                    StreamWriter sw = null;
                    sw = new StreamWriter(FileFullPath, false);

                    // Write the Header Row to File
                    int ColumnCount = d_table.Columns.Count;
                    for (int ic = 0; ic < ColumnCount; ic++)
                    {
                        sw.Write(d_table.Columns[ic]);
                        if (ic < ColumnCount - 1)
                        {
                            sw.Write(FileDelimiter);
                        }
                    }
                    sw.Write(sw.NewLine);

                    // Write All Rows to the File
                    foreach (DataRow dr in d_table.Rows)
                    {
                        for (int ir = 0; ir < ColumnCount; ir++)
                        {
                            if (!Convert.IsDBNull(dr[ir]))
                            {
                                sw.Write(dr[ir].ToString());
                            }
                            if (ir < ColumnCount - 1)
                            {
                                sw.Write(FileDelimiter);
                            }
                        }
                        sw.Write(sw.NewLine);
                        
                    }
        string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
        try
        {

            //Declare Variables
  string DestinationFolder = Dts.Variables["User::DestinationFolder"].Value.ToString();
  string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
  string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();
           


            //USE ADO.NET Connection from SSIS Package to get data from table
            SqlConnection myADONETConnection = new SqlConnection();
            myADONETConnection = (SqlConnection)(Dts.Connections["DBConn"].AcquireConnection(Dts.Transaction) as SqlConnection);


            //Read list of Tables with Schema from Database
          string query = "SELECT Schema_name(schema_id) AS SchemaName,name AS TableName FROM   sys.tables WHERE  is_ms_shipped = 0";

            //MessageBox.Show(query.ToString());
            SqlCommand cmd = new SqlCommand(query, myADONETConnection);
            DataTable dt = new DataTable();
            dt.Load(cmd.ExecuteReader());
            
            //Loop through datatable(dt) that has schema and table names
       foreach (DataRow dt_row in dt.Rows)
            {
                string SchemaName = "";
                string TableName = "";
                object[] array = dt_row.ItemArray;
                SchemaName = array[0].ToString();
                TableName = array[1].ToString();
               
               string FileFullPath =DestinationFolder +"\\"+ SchemaName+"_"+TableName + "_" + datetime+FileExtension;

            //Get the data for a table into data table 
            string data_query = "Select * From ["+SchemaName+"].["+TableName+"]";
            SqlCommand data_cmd = new SqlCommand(data_query, myADONETConnection);
            DataTable d_table = new DataTable();
            d_table.Load(data_cmd.ExecuteReader());
     
                    StreamWriter sw = null;
                    sw = new StreamWriter(FileFullPath, false);

                    // Write the Header Row to File
                    int ColumnCount = d_table.Columns.Count;
                    for (int ic = 0; ic < ColumnCount; ic++)
                    {
                        sw.Write(d_table.Columns[ic]);
                        if (ic < ColumnCount - 1)
                        {
                            sw.Write(FileDelimiter);
                        }
                    }
                    sw.Write(sw.NewLine);

                    // Write All Rows to the File
                    foreach (DataRow dr in d_table.Rows)
                    {
                        for (int ir = 0; ir < ColumnCount; ir++)
                        {
                            if (!Convert.IsDBNull(dr[ir]))
                            {
                                sw.Write(dr[ir].ToString());
                            }
                            if (ir < ColumnCount - 1)
                            {
                                sw.Write(FileDelimiter);
                            }
                        }
                        sw.Write(sw.NewLine);
                        
                    }
                    
                    sw.Close();                            
              
                Dts.TaskResult = (int)ScriptResults.Success;
            }

        }

        catch (Exception exception)
        {

            // Create Log File for Errors
            using (StreamWriter sw = File.CreateText(Dts.Variables["User::LogFolder"].Value.ToString() + "\\" +
                "ErrorLog_" + datetime + ".log"))
            {
                sw.WriteLine(exception.ToString());
                Dts.TaskResult = (int)ScriptResults.Failure;


            }
        }

【问题讨论】:

  • sw.Write(FileDelimiter); 这不是加你分隔符的具体代码吗?反过来,该字符串变量设置为string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();。这是一个包变量 - 在您的链接的第一步中讨论。不知何故,我怀疑您是否希望将逗号和管道都用作字段分隔符 - 但只有您才能知道。
  • 如果字段中有逗号,您只需要管道。这是 c# ...... field.Contains(",") ? “|” + 字段 +“|” : 字段

标签: c# sql-server ssis


【解决方案1】:

该包是否为获取数据而编写了某种硬代码查询,以及哪些内容可用于导出过程?如果是这样,数据是否仅用于查询中的导出过程?还是查询数据,显示一些报告,然后允许从相同结果导出到逗号分隔列表?

我知道,一堆问题。但是,如果您的查询专门用于提取数据以进行导出,为什么不在查询数据时添加管道,例如

select
      SomeIntegerColumn,
      '|' + SomeStringField + '|' as SomeStringField,
      '|' + AnotherString + '|' as AnotherString
      SomeDateColumn,
      etc
   from
      ...

那么数据已经被格式化,可以开始使用了。

【讨论】:

  • 包从几个视图中提取数据并为每个视图导出一个平面文件。我想用管道字符修改视图,但后来想知道是否有另一种更好的方法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多