The following code is for reading from an Access DB and transferring to a SQL DB.
Can anyone tell me why this code just hangs after it is run ?
private void readWriteRefuseDay()
{
OleDbDataReader dr;
oCon = new OleDbConnection(oConStr);
sqlCon = new SqlConnection(sqlConStr);
oQuery = "SELECT UPRN, RefuseDay, RefuseWeek FROM RefuseDay";
sqlQuery = "INSERT INTO Ref_RefuseDay (UPRN, RefuseDay, RefuseWeek) VALUES (@UPRN, @RefuseDay, @RefuseWeek)";
oCmd = new OleDbCommand(oQuery, oCon);
string sUPRN;
string sRefuseDay;
Int32 iRefuseWeek;
try
{
oCon.Open();
sqlCon.Open();
count = 0;
lblProcessing.Text = count.ToString();
dr = oCmd.ExecuteReader();
while (dr.Read())
{
lblProcessing.Text = "Processing: RefuseDay " + count.ToString();
sUPRN = dr.GetString(0);
sRefuseDay = dr.GetString(1);
iRefuseWeek = dr.GetInt32(2);
sqlCmd = new SqlCommand(sqlQuery, sqlCon);
sqlCmd.Parameters.AddWithValue("@UPRN", sUPRN);
sqlCmd.Parameters.AddWithValue("@RefuseDay", sRefuseDay);
sqlCmd.Parameters.AddWithValue("@RefuseWeek", iRefuseWeek);
sqlCmd.ExecuteNonQuery();
count++;
}
dr.Close();
oCon.Close();
oCmd.Dispose();
oCon.Dispose();
sqlCon.Close();
sqlCmd.Dispose();
sqlCon.Dispose();
}
catch (Exception ex) { MessageBox.Show(ex.Message); }
I have a label that should change telling me the row that is being processed but the Form just sits and becomes unresponsive once I have clicked the button.
Everything works, its just the form that freezes.
I'm open to any and all suggestions to improve the code also. I've noticed people using:
using (SqlConnection sqlCon = new SqlConnection(connectionString))
for example. Never really understood this. Is it better ? I have seen it used for SqlCommand objects too.
Any way this code can be improved, lets hear it.
Thanks in advance.