views:

45

answers:

2

Hi,

I'm executing a SQL stored procedure from a c# code but it is not responding at all. There is no exception or error generated. The processing as if gets hung. The stored procedure consists of multiple update statements and a select statement. The stored procedure is running fine independently and takes about 3-5 minutes to execute whereas when called from the C# code it is not responding even after 20 mins or more. When I comment most of the updates statements one run only one or two the executenonquery works. I have even increased the commandTimeout time.

Please suggest as this is something urgent. Please find below the C# code:

C# function:

private void PanDatabase(DateTime StartDate, DateTime EndDate)
    {
        SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["connectionString"]);

        SqlCommand cmd = new SqlCommand("PanData", conn);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        cmd.Parameters.Add(new SqlParameter("@start_date", StartDate.ToShortDateString()));
        cmd.Parameters.Add(new SqlParameter("@end_date", EndDate.ToShortDateString()));
        cmd.Parameters.Add(new SqlParameter("@period_status", _periodPan));

        conn.Open();

        cmd.CommandTimeout = 9000;

        cmd.ExecuteNonQuery();

        cmd.Dispose();
        conn.Close();
        conn.Dispose();
    }
A: 

I agree with Mike's comment above. On occasions where I have needed to spawn a long running SQL query from a web application I normally do so by having a table of pending jobs. Then setting up a SQL Agent job to check this periodically and run the procedure if required. This can write back to the table then the web application can feed back some progress information to the user either by AJAX or simply on page refresh.

Edit: But to answer your actual question have you tried to use SQL Profiler to see what's happening on the server?

Martin Smith
Just to add one more thing, not sure if it wil be of any help but, it is a windows service which is callin th SP and hosted on the database server itself.I have not tried SQL Profiler... Would do that. But why is it that it is a problem when the SP is running successfully independently. I tried updating a table while running the query via the c# code but it did not work. There was no data added to it at all.
Ridhi
Ah that changes things. On reason why you might be getting different behaviour is different SET options on the connections leading to you getting different cached execution plans. I've encountered similar issues with parameter sniffing before where a query in Management Studio worked fine but not from my application. Again Profiler can help you track this sort of issue down.
Martin Smith
When you say "I tried updating a table while running the query via the c# code but it did not work. There was no data added to it at all" do you mean that you have tried different queries and none of them work from your Windows Service?
Martin Smith
+1  A: 

By default your .Net code is running in a Serializable transaction. Could that be causing some locking? Within a single Stored Proc, I don't see this being an issue, but if you are calling multiple DB queries from .Net, then they may be interfering with each other.

You can verify the locking by looking at the job status in DB Manager "Activity Monitor"*. More detailed diagnosis should be possible with SQL Profiler, as Martin suggests. (*Only if you have sufficient DB permissions.)

Check for multiple calls to the same Stored Proc by adding some System.Diagnostics.Trace statements to the .Net code.

Jennifer Zouak