SSIS: с помощью задачи «Сценарий» импортируйте CSV-файл (с разделителем файлов как и текстовым квалификатором как «) в таблицу сервера sql - PullRequest
0 голосов
/ 03 октября 2018

Я хочу знать, как реализовать классификатор текста при импорте с помощью задачи скрипта.Ниже мой код.Я был в состоянии использовать только разделитель, а не текстовый классификатор.Итак, я также получаю двойные кавычки, загруженные в мою таблицу.

        public void Main()
    {
        // TODO: Add your code here
        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 TableName = Dts.Variables["User::DestinationTable"].Value.ToString();


        SqlConnection myADONETConnection = new SqlConnection();
        myADONETConnection = (SqlConnection)
        (Dts.Connections["PEGASUS.AdventureWorks1"].AcquireConnection(Dts.Transaction) as SqlConnection);

       string[] fileEntries = Directory.GetFiles(SourceFolderPath, "*" + FileExtension);
            foreach (string fileName in fileEntries)
            {
                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(FileDelimiter, "],[").Replace("\"", ""))+ "]";
                        MessageBox.Show(ColumnList.ToString());
                    }
                    else
                    {
                        MessageBox.Show("pass 2");
                        string query = "Insert into " + TableName + " (" + ColumnList + ") ";
                        query += "VALUES('" + line.Replace(FileDelimiter, "','") + "')";
                        MessageBox.Show("pass 3");

                        //MessageBox.Show(query.ToString());
                        SqlCommand cmd = new SqlCommand(query, myADONETConnection);
                        cmd.ExecuteNonQuery();
                        MessageBox.Show("pass 4");
                    }

                    counter++;
                }

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

CSV input This is expected output

выше - входи ожидаемый результат.

...