patterncsharpMinor
Storing synced results of a MSSQL database into a MySQL database
Viewed 0 times
syncedintomssqldatabasemysqlresultsstoring
Problem
I'm fairly new to C# and I thought this program was so simple it was a great candidate for a good ol' code review. This program syncs the results of one MSSQL database and puts them into a MySQL database based on the datetime value. I always tend to do things the hard way when I start off on a program and I am always seeking to improve this. Let me know if you see anything that needs improvement or if I did anything dumb.
Main Program
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace TransferOfData
{
public class TransferData
{
static void Main(string[] args)
{
ErrorLogger el = new ErrorLogger();
InitializeDB idb = new InitializeDB();
MySqlDataReader myrdr = null;
SqlDataReader msrdr = null;
string strLastDBUpdate = null;
idb.OpenDatabases();
//Run the query that will collect the last date the database was updated from the mssql database
try
{
string sqlLastDBUpdate = "select max(dIntervalStart) intervalStart from forecasts.statistics";
MySqlCommand mycmd = new MySqlCommand(sqlLastDBUpdate, idb.myconn);
myrdr = mycmd.ExecuteReader();
if (myrdr.Read())
{
strLastDBUpdate = myrdr.GetString(0);
}
myrdr.Close();
}
catch (Exception e)
{
el.errorCode = 2;
el.errorDescription = e.ToString();
el.createError();
}
//Now we will get all of the results from the mssql database and transfer them to the mysql database
try
{
string strCatchUp = "select dIntervalStart, nExterntoInternAcdCalls, cName " +
Main Program
```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
namespace TransferOfData
{
public class TransferData
{
static void Main(string[] args)
{
ErrorLogger el = new ErrorLogger();
InitializeDB idb = new InitializeDB();
MySqlDataReader myrdr = null;
SqlDataReader msrdr = null;
string strLastDBUpdate = null;
idb.OpenDatabases();
//Run the query that will collect the last date the database was updated from the mssql database
try
{
string sqlLastDBUpdate = "select max(dIntervalStart) intervalStart from forecasts.statistics";
MySqlCommand mycmd = new MySqlCommand(sqlLastDBUpdate, idb.myconn);
myrdr = mycmd.ExecuteReader();
if (myrdr.Read())
{
strLastDBUpdate = myrdr.GetString(0);
}
myrdr.Close();
}
catch (Exception e)
{
el.errorCode = 2;
el.errorDescription = e.ToString();
el.createError();
}
//Now we will get all of the results from the mssql database and transfer them to the mysql database
try
{
string strCatchUp = "select dIntervalStart, nExterntoInternAcdCalls, cName " +
Solution
you should use some
You can also put
and then you don't actually need the closing statement because the
You should also take advantage of the
it would look something like this if you decide to use the
with the closing inside the
just change the Method Declaration to:
Using statements so that your connections are disposed of no matter what happens with the code, like if there is an exception.string sqlLastDBUpdate = "select max(dIntervalStart) intervalStart from forecasts.statistics";
Using (mycmd = new MySqlCommand(sqlLastDBUpdate, idb.myconn))
{
myrdr = mycmd.ExecuteReader();
if (myrdr.Read())
{
strLastDBUpdate = myrdr.GetString(0);
}
myrdr.Close();
}You can also put
myrdr = mycmd.ExecuteReader(); into a using like thisstring sqlLastDBUpdate = "select max(dIntervalStart) intervalStart from forecasts.statistics";
Using (mycmd = new MySqlCommand(sqlLastDBUpdate, idb.myconn))
{
Using (myrdr = mycmd.ExecuteReader())
{
if (myrdr.Read())
{
strLastDBUpdate = myrdr.GetString(0);
}
}
}and then you don't actually need the closing statement because the
Using does the closing automatically.You should also take advantage of the
try catch's Finally statement, which you actually won't need if you use a Using statement because it automatically closes the connections.it would look something like this if you decide to use the
Try Catch Finallytry
{
string sqlLastDBUpdate = "select max(dIntervalStart) intervalStart from forecasts.statistics";
MySqlCommand mycmd = new MySqlCommand(sqlLastDBUpdate, idb.myconn);
myrdr = mycmd.ExecuteReader();
if (myrdr.Read())
{
strLastDBUpdate = myrdr.GetString(0);
}
}
catch (Exception e)
{
el.errorCode = 2;
el.errorDescription = e.ToString();
el.createError();
}
finally
{
myrdr.Close();
}with the closing inside the
try statement, you don't need it there when you put it in the finally, inside the finally it will be closed no matter what happens with your code.
I don't know what your InitializeDB idb = new InitializeDB(); is made up of, but if it makes use of the IDisposable interface you could also use that in a Using statement surrounding your data reader Using statement that way no matter where the exception happens the connection is disposed of and so it the reader, and they would be disposed in the order that they need to be.
instead of using a StreamWriter.Write() you could use a StreamWriter.WriteLine() and get rid of an extra variable in the function.
public void createError()
{
string logPath = @"C:\Logs\Scheduler\transferFileLog.txt";
Using (StreamWriter sw = File.AppendText(logPath))
{
sw.WriteLine("TransferData Error code: " + errorCode);
sw.WriteLine("Description:");
sw.WriteLine(errorDescription);
sw.WriteLine("------------------------------------------------------------\n");
//sw.Write(errorFinal);
}
Console.WriteLine("An error has occurred. Check the error log at " + logPath + " for further information.");
}
you should add parameters to this function as well, then you can send the variables errorCode and errorDescription` to it and take care of it in one swift movement, then you can also get rid of the property declarations, I don't think they are necessary.just change the Method Declaration to:
public void CreateError(int errorCode, string errorDescription)Code Snippets
string sqlLastDBUpdate = "select max(dIntervalStart) intervalStart from forecasts.statistics";
Using (mycmd = new MySqlCommand(sqlLastDBUpdate, idb.myconn))
{
myrdr = mycmd.ExecuteReader();
if (myrdr.Read())
{
strLastDBUpdate = myrdr.GetString(0);
}
myrdr.Close();
}string sqlLastDBUpdate = "select max(dIntervalStart) intervalStart from forecasts.statistics";
Using (mycmd = new MySqlCommand(sqlLastDBUpdate, idb.myconn))
{
Using (myrdr = mycmd.ExecuteReader())
{
if (myrdr.Read())
{
strLastDBUpdate = myrdr.GetString(0);
}
}
}try
{
string sqlLastDBUpdate = "select max(dIntervalStart) intervalStart from forecasts.statistics";
MySqlCommand mycmd = new MySqlCommand(sqlLastDBUpdate, idb.myconn);
myrdr = mycmd.ExecuteReader();
if (myrdr.Read())
{
strLastDBUpdate = myrdr.GetString(0);
}
}
catch (Exception e)
{
el.errorCode = 2;
el.errorDescription = e.ToString();
el.createError();
}
finally
{
myrdr.Close();
}public void createError()
{
string logPath = @"C:\Logs\Scheduler\transferFileLog.txt";
Using (StreamWriter sw = File.AppendText(logPath))
{
sw.WriteLine("TransferData Error code: " + errorCode);
sw.WriteLine("Description:");
sw.WriteLine(errorDescription);
sw.WriteLine("------------------------------------------------------------\n");
//sw.Write(errorFinal);
}
Console.WriteLine("An error has occurred. Check the error log at " + logPath + " for further information.");
}public void CreateError(int errorCode, string errorDescription)Context
StackExchange Code Review Q#48672, answer score: 7
Revisions (0)
No revisions yet.