Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.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# SSI导入多个文件忽略空行_C#_Sql Server_Ssis - Fatal编程技术网

C# SSI导入多个文件忽略空行

C# SSI导入多个文件忽略空行,c#,sql-server,ssis,C#,Sql Server,Ssis,我在SSIS中有以下脚本任务,可以将多个文本文件导入到多个表中。它根据文本文件名动态创建表 但是,某些文本文件的结尾有一行或多行空行-如何排除这些空行?文本文件来自第三方,因此我无法在源代码处解析 这是我的代码: { SqlConnection myADONETConnection = new SqlConnection(); myADONETConnection = (SqlConnection)(Dts.Connections["xxxxxxx

我在SSIS中有以下脚本任务,可以将多个文本文件导入到多个表中。它根据文本文件名动态创建表

但是,某些文本文件的结尾有一行或多行空行-如何排除这些空行?文本文件来自第三方,因此我无法在源代码处解析

这是我的代码:

{
            SqlConnection myADONETConnection = new SqlConnection();
            myADONETConnection = (SqlConnection)(Dts.Connections["xxxxxxxxx"].AcquireConnection(Dts.Transaction) as SqlConnection);
         //   MessageBox.Show(myADONETConnection.ConnectionString, "xxxxxxxxxx");

            string line1 = "";
            //Reading file names one by one
            string SourceDirectory = @"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
            // TODO: Add your code here
            string[] fileEntries = Directory.GetFiles(SourceDirectory);
            foreach (string fileName in fileEntries)
            {
                // do something with fileName
             //   MessageBox.Show(fileName);
                string columname = "";


                //Reading first line of each file and assign to variable
                System.IO.StreamReader file2 =
                new System.IO.StreamReader(fileName);


                string filenameonly = ((((fileName.Replace(SourceDirectory, "")).Replace(".txt", "")).Replace("\\", "")).Replace("-", "_"));
                line1 = (" IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo]." + filenameonly + "') AND type in (N'U'))DROP TABLE [dbo]." + filenameonly + " Create Table dbo." + filenameonly + "([" + file2.ReadLine().Replace("|", "] NVARCHAR(500),[") + "] NVARCHAR(500))").Replace(".txt", "");


                file2.Close();

             //   MessageBox.Show(line1.ToString());

                SqlCommand myCommand = new SqlCommand(line1, myADONETConnection);
                myCommand.ExecuteNonQuery();          

             //   MessageBox.Show("TABLE IS CREATED");

                //Writing Data of File Into Table
                int counter = 0;
                string line;

                System.IO.StreamReader SourceFile =
                new System.IO.StreamReader(fileName);
                while ((line = SourceFile.ReadLine()) != null)
                {

                    if (counter == 0)
                    {
                        columname = line.ToString();
                        columname = "[" + columname.Replace("|", "],[") + "]";
                      //  MessageBox.Show(columname);
                      //  MessageBox.Show("INside IF");
                    }

                    else
                    {
                        //  MessageBox.Show("Inside ELSE");


                    while ((line = SourceFile.ReadLine()) != null)
                    {
                        line = line.Trim();
                        if (line.Length > 0)
                        {
                            string query = "Insert into dbo." + filenameonly + "(" + columname + ") VALUES('" + line.Replace("|", "','") + "')";

                            SqlCommand myCommand1 = new SqlCommand(query, myADONETConnection);
                            myCommand1.ExecuteNonQuery();
                        }
                    }


                    }

                    counter++;

                }

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

我通常检查行的长度是否大于0

            while ((line = SourceFile.ReadLine()) != null)
            {
                line = line.Trim();
                if (line.Length > 0)
                {
                    //put your code here
                }
            }

也许是通过检查行的长度,<代码>行。在插入命令感谢之前,替换(“y”,“”)。长度< /代码>,我这样做,它忽略了空白行——但是,它现在也忽略了每个导入中的列标题之后的第一行数据。你必须有计数器++;在错误的地方。我在其他地方试过,然后它就什么都没有了?它当前位于上次编辑的else之后。为什么有两个WHILE循环?您应该只有一个。因为我正在基于文本文件的第一行创建SQL表的第一行。第一个用于列标题,第二个用于实际细节