If statement pertaining to database criteria
I am brand new to this community. I am also somewhat new to programming and this is a noob question. If my wording is bad or any members have any advice to give a newbie please do! Anyway, here I go!
The following method (although its obvious), grabs a file and inserts it into a database table (KC_Reply_Shipped), (or cancels into another table), also obvious. The file looks something like this
YYYY847583 blahblahblah blah blahahasdf blah blah
XXX8343445 " " " "
XXX8976556 " " " "
However, I do NOT want to insert the YYYY. I can not help that the File has this, because it is out of my control. But I DO want the XXX. Sometimes the file will have YYYY and sometimes it will not.
although this seems so easy, me being noob, I get confused on the wording because I already have a while statement and the method is boolean. FYI this is only for fun and experimentation. Nothing serious. Any help or answer would be greatly appreciated. Thank you^2 in advance!
private bool InsertIntoDatabase(FileInfo file)
{
string table = "KC_Reply_Shipped";
string line;
StreamReader reader = new StreamReader(file.FullName);
try
{
Database db = new Database();
ShippedOrders = new List<string[]>();
CancelledOrders = new List<string[]>();
while ((line = reader.ReadLine()) != null)
{
string[] temp = GetFileDataForInsert(line);
temp[temp.Length - 1] = file.Name.Substring(0, file.Name.Length - 4);
if (temp[1] == "30")
{
table = "KC_Reply_Cancelled";
CancelledOrders.Add(temp);
db.UpdateOrderToCancelled(temp[0]);
}
else
开发者_StackOverflow {
ShippedOrders.Add(temp);
db.UpdateOrderToShipped(temp[0]);
db.UpdateOrderTrackingNumber(temp[10]);
db.UpdateOrderNotes(temp);
}
db.BatchInsert(table, temp);
}
}
catch (Exception e)
{
errorMsg = e.Message;
return false;
}
finally
{
reader.Close();
}
return true;
}
You could try something like the following to check the start of the line and the make a decision from there:
if(!line.StartsWith("YYYY"))
{
//....do stuff here
}
You can skip the lines starting with YYYY like this:
...
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("YYYY"))
continue;
else {
// your code
}
}
...
If you want to ignore the file in case there is a "YYYY" then you can do this:
...
while ((line = reader.ReadLine()) != null)
{
if (line.StartsWith("YYYY"))
return true; // or false, whatever you method should return in this case
else {
// your code
}
}
...
精彩评论