.NET - SQL Multiple things going on at the same time.

Status
Not open for further replies.

sryan2k1

Ars Legatus Legionis
46,882
Subscriptor++
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.

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();
                    }
 

sryan2k1

Ars Legatus Legionis
46,882
Subscriptor++
There are only 3 functions that access the database, and I wrapped each one in their own

Code:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString());
conn.open();

It seems like connection pooling works that way, I haven't seen it throw an exception all day.

I could probbly reduce it to two, but I want to test it like this for a while.
 

technophile

Ars Legatus Legionis
23,768
Wait, you were using the same SqlConnection object from multiple simultaneous threads?

Yeah, that's not a good idea; SqlConnections aren't thread-safe. Using individual connections like you're doing is the proper way to do it; connection pooling will make sure you don't suffer performance-wise from doing so.

Make sure you wrap the SqlConnection and SqlCommand objects in using() blocks so they get closed when you're done.
 

BlissHunter-R

Wise, Aged Ars Veteran
103
sryan2k1":3obpjwkg said:
There are only 3 functions that access the database, and I wrapped each one in their own

Code:
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["dbConnection"].ToString());
conn.open();

It seems like connection pooling works that way, I haven't seen it throw an exception all day.

I think by default, Windows 2003 Server IIS holds connections open for 20 minutes and can only 16 in its pool. In addition to use using "using" (heh, alliteration ) blocks, you can also do a
Code:
finally
{
conn.close();
}
....after every try.
 
Status
Not open for further replies.