I have an OleDbCommand for my inserts that I have tried to implement to avoid SQL injection. Before that I used simple strings for my queries and I didn't like that. Now my piece of code for inserting records looks like this:
try
{
OleDbConnection rConn = new OleDbConnection(args[3]);
rConn.Open();
using (OleDbCommand insert = new OleDbCommand(String.Format(Globals.QUERY_INSERT_CLICK, args[4]), rConn))
{
insert.Parameters.Add("id", OleDbType.BigInt, 20);
insert.Parameters.Add("email", OleDbType.VarChar, 255);
insert.Parameters.Add("clickTime", OleDbType.Date, 20);
insert.Parameters.Add("subscriberId", OleDbType.BigInt, 20);
insert.Parameters.Add("link", OleDbType.VarChar, 255);
insert.Parameters.Add("sendQueueId", OleDbType.BigInt, 20);
insert.Parameters.Add("mailingListName", OleDbType.VarChar, 255);
insert.Parameters.Add("newsletterId", OleDbType.BigInt, 20);
insert.Parameters.Add("sendDate", OleDbType.Date, 20);
insert.Parameters[0].Value = clickitem.Id;
insert.Parameters[1].Value = clickitem.Email;
insert.Parameters[2].Value = clickitem.ClickTime;
insert.Parameters[3].Value = clickitem.SubscriberId;
insert.Parameters[4].Value = clickitem.Link;
insert.Parameters[5].Value = clickitem.SendQueueId;
insert.Parameters[6].Value = mailingListName;
insert.Parameters[7].Value = newsletterID;
insert.Parameters[8].Value = sendDate;
insert.Prepare();
insert.ExecuteNonQuery();
}
rConn.Close();
}
catch (OleDbException oldbex)
{
logger.WriteToLog("GETCLICKS", "OleDbException: " + Globals.ERROR_INSERT_CLICK + oldbex.Message);
}
catch (Exception ex)
{
logger.WriteToLog("GETCLICKS", Globals.ERROR_INSERT_CLICK + ex.Message);
}
I have thousands of inserts and I see from my log that some of them are not correctly inserted. The exception tells me e.g. cannot convert from bigint to datetime and stuff like that. Although most of my records are inserted correctly, I want to know which of these insert queries exactly caused the error. How can I figure that out?
N.B. Before using this method I had access to my query string and I found the error instantly. Now I guess my immunity to SQL injection is causing some confusion for myself