无法使用c#

时间:2016-05-27 15:10:24

标签: c# sql-server

我写了一个代码,它将读取多个分隔的txt文件,并将通过它们解析数据,然后应该将它们添加到我的SQL表中的行。我遇到的问题是它将读入所有文件并解析它们(我知道它正在这样做,因为目前我还将它写入控制台并且它正在编写我需要的所有值。它应该是它完成后将文件移动到另一个文件夹,这也是因为它运行后文件只在单独的文件夹中。我遇到的问题是我的代码只会在SQL表中添加一行。如果全部行是空的,我可以用一个文件运行代码,它将生成行并添加数字。但是,如果我再次运行它,代码将解析数据但不会为数字创建一个新行。因此,当我尝试使用多个文件时,它只添加第一个文件,然后继续运行并解析其余文件,但不会将它们添加到表中

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FileHelpers;
using System.Data.SqlClient;
using System.IO;
using System.Data;

namespace prototype_using_filehelpers
{

    class ManagerReport
    {

        [DelimitedRecord("|")]
        [IgnoreEmptyLines()]
        [IgnoreFirst()]
        [IgnoreLast(2)]


        public class ManagerReportNames
        {


            public int? MASTER_VALUE_ORDER;
            public int? MASTER_VALUE;
            public string RESORT;
            public int? CS_HEADING_COUNT_MASTER;
            public int? CS_FS_ARR_ROOMS_MASTER;
            public int? CS_FS_DEP_ROOMS_MASTER;
            public int? CS_FS_NO_ROOMS_MASTER;
            public int? CS_FS_GUESTS_MASTER;
            public int? CS_FS_TOTAL_REVENUE_MASTER;
            public int? CS_FS_ROOM_REVENUE_MASTER;
            public int? CS_FS_INVENTORY_ROOMS_MASTER;
            public int? CF_FS_PERC_OCC_ROOMS_MASTER;
            public int? CF_FS_AVG_ROOM_RATE_MASTER;
            public int? LAST_YEAR_01;
            public int? SUB_GRP_1_ORDER;
            public string SUB_GRP_1;
            public string DESCRIPTION;
            public string AMOUNT_FORMAT_TYPE;
            public string PRINT_LINE_AFTER_YN;
            public int? HEADING_1_ORDER;
            public int? HEADING_1;
            [FieldOptional]
            public string HEADING_2;
            [FieldOptional]
            [FieldConverter(ConverterKind.Decimal, ".")]
            public decimal? SUM_AMOUNT;
            [FieldOptional]
            public decimal? FORMATTED_AMOUNT;


        }

        static void ProcessFilesCSVFiles(string originalPath, string destinationPath)
        {


            // first check if path exists
            if (!Directory.Exists(originalPath))
                // doesn't exist then exit, can't copy from something that doesn't exist
                return;
            var copyPathDirectory = new DirectoryInfo(originalPath);
            // using the SearchOption.AllDirectories will search sub directories
            var copyPathCSVFiles = copyPathDirectory.GetFiles("*.txt", SearchOption.AllDirectories);
            // loops through directory looking for txt files
            for (var i = 0; i < copyPathCSVFiles.Length; i++)
            {
                // get the file
                var csvFile = copyPathCSVFiles[i];
                //sets lines to the files full extention so it can then be called at a later time
                string lines = csvFile.FullName;

                // read the csv file line by line
                FileHelperEngine engine = new FileHelperEngine(typeof(ManagerReportNames));
                var records = engine.ReadFile(lines) as ManagerReportNames[];


                foreach (var record in records)
                {
                    Console.WriteLine(record.RESORT);
                    Console.WriteLine(record.FORMATTED_AMOUNT);

                    string Heading = record.HEADING_2;
                    string Group = record.SUB_GRP_1;
                    string Description = record.DESCRIPTION;
                    string column;
                    decimal? amount;

                    if (Heading == "DAY" && Group == "OCC_PERC")
                    {
                        column = "Percent_Rooms_Occupied";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "OCC_PERC_WO_CH")
                    {
                        column = "Percent_Rooms_Occupied_minus_Comp_and_House";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "ADR_ROOM")
                    {
                        column = "ADR";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "ADR_ROOM_WO_CH")
                    {
                        column = "ADR_minus_Comp_and_House";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "ROOMREV_AVL_ROOMS_MINUS_OOO")
                    {
                        column = "Revenue_per_Available_Room_minus_OOO";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "TOTAL_REVENUE")
                    {
                        column = "Total_Revenue";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "ROOM_REVENUE")
                    {
                        column = "Room_Revenue";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "FOOD_BEV_REVENUE")
                    {
                        column = "Food_And_Beverage_Revenue";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "OTHER_REVENUE" && Description == "Other Revenue")
                    {
                        column = "Other_Revenue";
                        amount = record.FORMATTED_AMOUNT;

                    }
                    else if (Heading == "DAY" && Group == "PHYSICAL_ROOMS")
                    {
                        column = "Total_Rooms_in_Hotel";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "OCC_ROOMS")
                    {
                        column = "Rooms_Occupied";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "OCC_MINUS_COMP_HU")
                    {
                        column = "Rooms_Occupied_minus_Comp_and_House_Use";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else if (Heading == "DAY" && Group == "COMP_ROOMS")
                    {
                        column = "Complimentary_Rooms";
                        amount = record.FORMATTED_AMOUNT;
                    }
                    else
                    {
                        continue;
                    }



                    // SQL connection. Creates connection and command and inserts the values taken from the File Helper engine into the SQL table
                    SqlCommand cmd;
                    SqlConnection conn;


                    conn = new SqlConnection("Data Source=hureports01;Initial Catalog=hureports;Integrated Security=True");
                    conn.Open();


                    var sqlCommand = string.Format(@"MERGE [HEWreport] AS target USING (select @Property_ID as Property_ID, @val as {0}) AS source ON (target.Property_ID = source.Property_ID) WHEN MATCHED THEN UPDATE SET {0}= source.{0}
                                                    WHEN NOT MATCHED THEN INSERT (Property_ID, {0}) VALUES (source.Property_ID, source.{0});", column);
                    cmd = new SqlCommand(sqlCommand, conn);
                    cmd.Parameters.AddWithValue("@Property_ID", record.RESORT);
                    cmd.Parameters.AddWithValue("@val", amount);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }





                // creates a variable that combines the the directory of the new folder with the file name
                var destinationFilePath = Path.Combine(destinationPath, csvFile.Name);
                // This loop prevents duplicates. If a file is already in the folder, it will delete the file already in there and move this one in.
                // Shouldn't be an issue since each file will have a different name
                if (File.Exists(destinationFilePath))
                {
                    File.Delete(destinationFilePath);
                }
                // moves it to the new folder
                csvFile.MoveTo(destinationFilePath);


            }
        }


        static void Main(string[] args)
        {

            ProcessFilesCSVFiles(@"C:\Users\Documents\Hotels", @"C:\Users\Documents\Completed Hotels");


        }
    }
}

已编辑,以显示我尝试使用的Insert语句执行的操作:

var sqlCommand = string.Format("INSERT INTO HEWreport (Property_ID, {0}) VALUES (@Property_ID,@val)", column);
cmd = new SqlCommand(sqlCommand, conn);
cmd.Parameters.AddWithValue("@Property_ID", record.RESORT);
cmd.Parameters.AddWithValue("@val", amount);

这个问题是每个金额值都在不同的行上。我希望同一文件中的13个金额值都在同一行。我只希望从另一个文件获取的值在不同的行上。为了获得我测试的一个文件的同一行的值,我必须创建一个MERGE语句。现在我想要实现更多文件,MERGE不起作用,但这两者都没有,因为没有任何东西在彼此相同的行上。我只是想让它在一个文件中读取,将一个文件中的所有内容放在同一行但在不同的列中,然后将下一个文件中的所有数据放入自己的行中。

这是使用insert:

13  TTNEW   5/27/2016   39.3    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
14  TTNEW   5/27/2016   NULL    37.31   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
15  TTNEW   5/27/2016   NULL    NULL    97.23   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
16  TTNEW   5/27/2016   NULL    NULL    NULL    102.42  NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
17  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    41.75   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL
18  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    NULL    10694.25    NULL    NULL    NULL    NULL    NULL    NULL    NULL
19  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    NULL    NULL    7681.26 NULL    NULL    NULL    NULL    NULL    NULL
20  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    NULL    NULL    NULL    2828.94 NULL    NULL    NULL    NULL    NULL
21  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    184.05  NULL    NULL    NULL    NULL
22  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    201 NULL    NULL    NULL
23  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    79  NULL    NULL
24  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    75  NULL
25  TTNEW   5/27/2016   NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    4
NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL    NULL

这是使用Merge:

12  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4

这是我的问题:

12  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
13  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
14  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
15  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
16  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
17  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
18  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
19  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
20  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
21  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
22  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
23  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
24  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4
25  TTNEW   5/27/2016   39.3    37.31   97.23   102.42  41.75   10694.25    7681.26 2828.94 184.05  201 79  75  4

当我将其切换回合并并给它3个文件进行排序时,它从第一个获取信息,并用它的数据覆盖所有现有数据。但是,我知道循环部分有效,因为当我使用它作为插入而不是合并时,所有文件中的所有数据都会显示出来,它最终会成为很多不必要的行。

0 个答案:

没有答案