Okay so, I'm currently running this code to move a bunch of data from a pervasive database into an access database
public List<HBPData> LoadData()
{
loadConnect(); //<-- get's the Pervasive/Access string from a text file
List<HBPData> listofhbpdata1 = new List<HBPData>();
List<HBPData> listofhbpdata2 = new List<HBPData>();
PsqlConnection myConnection = new PsqlConnection();
myConnection.ConnectionString = PervasiveString;
myConnection.Open();
PsqlCommand myCommand = new PsqlCommand("Select NUMBER, CUST_NAME, PO_NO, REQD_DATE, PO_NO, CUST_PO_NO, ORD_DATE, STATUS FROM SALES_ORDER_HEADER WHERE ORD_DATE > 20120220 Order By ORD_DATE desc", myConnection);
PsqlDataReader myreader = null;
myreader = myCommand.ExecuteReader();
while (myreader.Read())
{
HBPData DataEntity = new HBPData();
DataEntity.NUMBER = (myreader["NUMBER"].ToString());
DataEntity.CUST_NO = (myreader["CUST_NAME"].ToString()).Replace("'","");
DataEntity.PO_NO = (myreader["PO_NO"].ToString());
DataEntity.RequiredDateTime = (myreader["REQD_DATE"].ToString());
DataEntity.Tag = (myreader["PO_NO"].ToString());
DataEntity.Shape = (myreader["CUST_PO_NO"].ToString());
DataEntity.ExpectedCompletion = myreader["ORD_DATE"].ToString().Substring(0, 4) + "/" + myreader["ORD_DATE"].ToString().Substring(4, 2) + "/" + myreader["ORD_DATE"].ToString().Substring(6, 2);
DataEntity.MostRecentStatus = (myreader["STATUS"].ToString());
listofhbpdata1.Add(DataEntity);
}
PsqlCommand myCommand1 = new PsqlCommand("Select NUMBER, RECNO, CODE, ORDD_DESCRIPTION, BVORDQTY FROM SALES_ORDER_DETAIL WHERE BVRVADDDATE > 20120220 AND (PROD_CODE = \'MET\' OR PROD_CODE = \'MDT\') Order By NUMBER desc", myConnection);
PsqlDataReader myreader1 = null;
myreader1 = myCommand1.ExecuteReader();
while (myreader.Read())
{
HBPData DataEntity = new HBPData();
DataEntity.NUMBER = (myreader1["NUMBER"].ToString());
DataEntity.RECNO = (myreader1["RECNO"].ToString());
DataEntity.CODE = (myreader1["CODE"].ToString());
DataEntity.DESCRIPTION = (myreader1["ORDD_DESCRIPTION"].ToString());
DataEntity.Quantity = (myreader1["BVORDQTY"].ToString());
listofhbpdata2.Add(DataEntity);
}
myConnection.Close();
myreader1.Close();
myreader.Close();
System.Data.OleDb.OleDbConnection myAccessConnection = new System.Data.OleDb.OleDbConnection();
myAccessConnection.ConnectionString = AccessString;
myAccessConnection.Open();
System.Data.OleDb.OleDbCommand myAccessCommand3 = new System.Data.OleDb.OleDbCommand("delete from AllOrders", myAccessConnection);
myAccessCommand3.ExecuteNonQuery();
for (int i = 0; i < listofhbpdata2.Count(); ++i)
{
System.Data.OleDb.OleDbCommand myAccessCommand2 = new System.Data.OleDb.OleDbCommand("" +
"Insert into AllOrders VALUES('" +
listofhbpdata2[i].NUMBER + "'" + ",'" + listofhbpdata2[i].RECNO.ToString() + "'" +
",'" + listofhbpdata2[i].CODE + "','" + listofhbpdata2[i].DESCRIPTION.Replace("\'", "F") + "'" +
",'" + listofhbpdata2[i].Quantity + "')", myAccessConnection);
myAccessCommand2.ExecuteNonQuery();
}
myAccessConnection.Close();
return listofhbpdata1;
}
Now,. If you look closely, I typo'd the 2nd reader, it should read while(myreader1.read()) ... i accidentally put myreader.read()
putting myreader.read() much to my surprise actually ran successfully... this is what kind of blew my mind,... i changed it to "myreader1.read()" and the run time of the code was almost double ...... , anyway, checking the database, all of the data was there.....
so, using common sense, i kind of figured well, it probably just does both sets of code every time it runs the first reader,
but then how come all of the data is there?
There's significantly less fields in the Sales_Order_Header than Sales_Order_Detail, if it's doing the reader for the first one, shouldn't it finish at the end of the header table and then stop? so why is all the data there?
Either way, the run time of this code is relatively slow, does anyone have any suggestions for improving my code?
Edit: Just to show that the 2nd reader is not infact returning false:
as you can see, the debugger has stepped inside of the reader