如何在例如| Ana“ ana” Ana |,| adda,adda |

时间:2019-05-31 14:40:15

标签: c# ssis

我正在进行sis集成,并在c#中编写了一个脚本任务,用于自动将csv文件导入数据库。它很好用,但是我需要帮助从csv中的管道(| aaa,aaa |或| a |,| a |)中的字符串中删除逗号。例如“地址,城市范围”。我想要一个可以删除该逗号()的函数。我将粘贴我的代码片段以及到目前为止的操作。

#
region Namespaces
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.IO;
//using CsvHelper.Configuration;
#
endregion

namespace ST_7ce5ad6fbc104157b534f4eb484a4417 {
 [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
 public partial class ScriptMain: Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase {
  public void Main() {
   string datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
   try {
    //Declare Variables
    string SourceFolderPath = Dts.Variables["User::SourceFolder"].Value.ToString();
    string FileExtension = Dts.Variables["User::FileExtension"].Value.ToString();
    string FileDelimiter = Dts.Variables["User::FileDelimiter"].Value.ToString();
    string ArchiveFolder = Dts.Variables["User::ArchiveFolder"].Value.ToString();
    string ColumnsDataType = Dts.Variables["User::ColumnsDataType"].Value.ToString();
    string SchemaName = Dts.Variables["User::SchemaName"].Value.ToString();
    SqlConnection myADONETConnection = new SqlConnection();
    myADONETConnection = (SqlConnection)
     (Dts.Connections["moviesdb"].AcquireConnection(Dts.Transaction) as SqlConnection);
    //Reading file names one by one
    string[] fileEntries = Directory.GetFiles(SourceFolderPath, "*" + FileExtension);
    foreach(string fileName in fileEntries) {
     //Writing Data of File Into Table
     string TableName = "";
     int counter = 0;
     string line;
     string ColumnList = "";
     //MessageBox.Show(fileName);
     System.IO.StreamReader SourceFile =
      new System.IO.StreamReader(fileName);
     while ((line = SourceFile.ReadLine()) != null) {
      if (counter == 0) {
       ColumnList = "[" + line.Replace("\"", "").Replace(FileDelimiter, "],[") + "]";
       //MessageBox.Show(ColumnList);
       //"[" + line.Replace(FileDelimiter, "],[") + "]";
       TableName = (((fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "")).Replace("\\", ""));
       string CreateTableStatement = "IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[" + SchemaName + "].";
       CreateTableStatement += "[" + TableName + "]')";
       CreateTableStatement += " AND type in (N'U'))DROP TABLE [" + SchemaName + "].";
       CreateTableStatement += "[" + TableName + "]  Create Table " + SchemaName + ".[" + TableName + "]";
       CreateTableStatement += "([" + line.Replace("\"", "").Replace(FileDelimiter, "] " + ColumnsDataType + ",[") + "] " + ColumnsDataType + ")";
       SqlCommand CreateTableCmd = new SqlCommand(CreateTableStatement, myADONETConnection);
       CreateTableCmd.ExecuteNonQuery();
       //MessageBox.Show(CreateTableStatement);
      } else {
       string query = "Insert into " + SchemaName + ".[" + TableName + "] (" + ColumnList + ") ";
       //query += "VALUES('" + line.Replace(FileDelimiter, "','").Replace("\"", "") + "')";
       //query += "VALUES('" + line.Replace(FileDelimiter, "','").Replace("\"", "").Replace("\"'\"", "") + "')";
       query += "VALUES('" + line.Replace("'", "").Replace(FileDelimiter, "','").Replace("\"", "") + "')";
       // MessageBox.Show(query.ToString());
       SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
       myCommand1.ExecuteNonQuery();
      }
      counter++;
     }
     SourceFile.Close();
     //move the file to archive folder after adding datetime to it
     File.Move(fileName, ArchiveFolder + "\\" + (fileName.Replace(SourceFolderPath, "")).Replace(FileExtension, "") + "_" + datetime + FileExtension);
     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;
    }
   }
  }#
  region ScriptResults declaration
  enum ScriptResults {
   Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
    Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
  };#
  endregion
 }
}

它实际上完成了我想要的并成功导入,但是在调整代码以替换csv文件中管道中字符串之间的逗号时,我需要进一步的帮助。

0 个答案:

没有答案