I'm a fairly new c# coder and I wrote a code that uses filehelpers to parse a csv and upload the data to a SQL server database. The issue I'm having is with a variable in my if-else statement.
Code below:
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;
namespace OnQ_prototype
{
class Report
{
[DelimitedRecord("\t")]
[IgnoreEmptyLines()]
public class ColumnReportNames
{
public String textbox22;
public String textbox29;
public String textbox24;
public String textbox23;
public String PSFullAcct;
public String AccountDescription;
public String BusDateAmount;
public String ThisPTDAmount;
public String LastPTDAmount;
public String ThisYTDAmount;
public String LastYTDAmount;
public String BusDatePctAvail;
public String ThisPTDPctAvail;
public String LastPTDPctAvail;
public String ThisYTDPctAvail;
public String LastYTDPctAvail;
}
static void ProcessFilesCSVFiles(string originalPath, string destinationPath)
{
foreach (var GenesisDirectory in Directory.GetDirectories(originalPath))
{
foreach (var inputFile in Directory.GetFiles(GenesisDirectory, "*.csv"))
{
string lines = inputFile;
FileHelperEngine engine = new FileHelperEngine(typeof(ColumnReportNames));
var records = engine.ReadFile(lines) as ColumnReportNames[];
foreach (var record in records)
{
Console.WriteLine(record.textbox24);
Console.WriteLine(record.textbox23);
Console.WriteLine(record.AccountDescription);
Console.WriteLine(record.BusDateAmount);
Console.WriteLine(record.BusDatePctAvail);
string Description = record.AccountDescription;
string column;
string amount;
string amount2;
if (Description == "Total Occupied ")
{
column = "Total_Occupied";
amount = record.BusDateAmount;
amount2 = record.BusDatePctAvail;
}
else if (Description == "Total Rooms Occupied")
{
column = "Total_Rooms_Occupied";
amount = record.BusDateAmount;
}
else if (Description == "Total Available Rooms")
{
column = "Total_Available_Rooms";
amount = record.BusDateAmount;
}
else if (Description == "Rooms Average Daily Rate")
{
column = "Rooms_Average_Daily_Rate";
amount = record.BusDateAmount;
}
else if (Description == "Total Net Rooms Revenue")
{
column = "Total_Net_Rooms_Revenue";
amount = record.BusDateAmount;
}
else if (Description == "Revenue Per Available Room")
{
column = "Revenue_Per_Available_Room";
amount = record.BusDateAmount;
}
else
{
continue;
}
SqlCommand cmd;
SqlConnection conn;
conn = new SqlConnection("Data Source=hureports01;Initial Catalog=hureports;Integrated Security=True");
conn.Open();
var sqlCommand = string.Format(@"MERGE [OnQReport] AS target USING (select @Property as Property, @Date_of_Report as Date_of_Report, @Percent_Occupancy_PAR as Percent_Occupancy_PAR, @val as {0}) AS source ON (target.Date_of_Report = source.Date_of_Report) WHEN MATCHED THEN UPDATE SET {0}= source.{0} WHEN NOT MATCHED THEN INSERT (Property, Date_of_Report, Percent_Occupancy_PAR, {0}) VALUES (source.Property, source.Date_of_Report, Percent_Occupancy_PAR, source.{0});", column);
cmd = new SqlCommand(sqlCommand, conn);
cmd.Parameters.AddWithValue("@Property", record.textbox24);
cmd.Parameters.AddWithValue("@Date_of_Report", record.textbox23);
cmd.Parameters.AddWithValue("@Percent_Occupancy_PAR", amount2);
cmd.Parameters.AddWithValue("@val", amount);
cmd.ExecuteNonQuery();
conn.Close();
}
var newFileName = DateTime.Now.ToString("MMddyyyy") + ".csv";
destinationPath = Path.Combine(GenesisDirectory, "Completed");
Directory.CreateDirectory(destinationPath);
var destinationFileName = Path.GetFileName(newFileName);
destinationPath = Path.Combine(destinationPath, destinationFileName);
File.Move(inputFile, destinationPath);
}
}
}
static void Main(string[] args)
{
try
{
ProcessFilesCSVFiles(@"C:\Users\btajfel\Documents\OnQ", @"C:\Users\btajfel\Documents\OnQ");
}
catch (System.Exception e)
{
Console.WriteLine("An error occurred: '{0}'", e);
Console.ReadLine();
}
}
}
}
If you look at my first if-statement, you can see that when that is the column name I set amount and amount2, to different values. In the rest of my else-if statements i give amount a value but not amount2. The reason for that is because I only want the value of record.BusDatePctAvail when the column is "Total Occupied ". Other than that, I don't need the values for record.BusDatePctAvail. The issue that I'm having is that in my statement cmd.Parameters.AddWithValue("@Percent_Occupancy_PAR", amount2); it says "use of unassigned local variable "amount2"". I believe the reason for this is because it is not assigned throughout the rest of the else-if statements but I may be wrong. If that is the issue, how would I fix this?
Aucun commentaire:
Enregistrer un commentaire