I'm having a problem with SQL (MSSQL) in .NET, likely due to my lack of DB knowledge, I can muddle through but I'm probably missing something obvious.
I've got a class/master DB thread that inserts data into a DB every so many seconds from a DataTable, this works fine, and is set up something like this.
The DataTable insert:
Another part of the code that can be called from a client thread looks like this, but sporadically I get errors that make it seem like running this command can screw up a bulk copy. Any idea? I've removed the identifying bits from the code, if you have any questions about what sections do what let me know.
Basically the datatable gets inserted every 10 seconds if it is not empty, and clients can call a function that runs the code below.
I've got a class/master DB thread that inserts data into a DB every so many seconds from a DataTable, this works fine, and is set up something like this.
Code:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString());
conn.open();
The DataTable insert:
Code:
using (SqlBulkCopy bulkcopy = new SqlBulkCopy(conn))
{
try
{
bulkcopy.DestinationTableName = "dbo.RawData";
bulkcopy.WriteToServer(vehicleData);
Console.WriteLine("Inserted {0} Records", vehicleData.Rows.Count);
vehicleData.Clear();
}
catch (Exception ee)
{
Console.WriteLine("DB Err: {0}", ee);
vehicleData.Clear();
conn.Close();
}
}
Another part of the code that can be called from a client thread looks like this, but sporadically I get errors that make it seem like running this command can screw up a bulk copy. Any idea? I've removed the identifying bits from the code, if you have any questions about what sections do what let me know.
Basically the datatable gets inserted every 10 seconds if it is not empty, and clients can call a function that runs the code below.
Code:
SqlCommand command = new SqlCommand();
command.Connection = conn;
command.CommandText = "select **********;";
command.Parameters.Add(new SqlParameter("@***", SqlDbType.VarChar));
command.Parameters["@***"].Value = ***;
try
{
int count = (int)command.ExecuteScalar();
if (count == 1)
{
command = new SqlCommand();
command.Connection = conn;
command.CommandText = "*******;";
// add the input parameter ...
command.Parameters.Add(new SqlParameter("@**", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@**", SqlDbType.VarChar));
command.Parameters.Add(new SqlParameter("@**", SqlDbType.DateTime));
// and set its value
command.Parameters["@**"].Value = **;
command.Parameters["@**"].Value = **;
command.Parameters["@**"].Value = **;
command.ExecuteNonQuery();
}