Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# LINQ Stored Procedure

I have a C# windows service that communicates with a SQL Server 2008 database via LINQ. Within LINQ, I have a few stored procedures defined. My service basically wakes up every couple of minutes and looks in the database for things to process. While processing, for each new record it runs a stored procedure. Once all of the records are processed, it runs another stored procedure. I have a very weird problem going on. For the first stored procedure (run with each record), things run fine. The procedure gets called, functions properly, and the code continues past the call. For the second procedure (run one all records processed), the service just hangs. I don't get an error message, it doesn't crash, it still seems to be running, but it never does anything until I restart the service. If I run the procedure manually in SQL Server Management Studio, it executes properly and completes. I am hoping somebody has an idea for what's going on here.

Within the loop of each record:

if (Settings.Default.SQLSpatialEnabled)
{
    try
    {
        if ((bool) f.sdrFaultType.TripFault)
        {
            DataContext.sp_locateFault ((int) f.ID);
        }
    }
    catch (Exception ex)
    {
        Logger.Logger.Trace ("Locate fault (" +
                             f.ID +
                             ") exception: " + 
                             ex.Message);
    }
}

After all records:

if (Settings.Default.SQLSpatialEnabled)
{
    DataContext.sp_mapFaults ();
    Logger.Logger.Trace ("Faults Mapped");
}

The 'Faults Mapped' never shows up in the log and everything basically stops.

like image 810
Nik Avatar asked Jun 22 '11 15:06

Nik


2 Answers

A few options perhaps:

  • indeed use the profiler to see if your SP is being executed at all or not;

  • if it IS being executed, perhaps a locking issue or a long processing time (different execution plans between manual and Linq execution?)

  • if it is NOT being executed add a Logger.Logger.Trace just before the DataContext.sp_mapFaults () to know for sure you get there and wrap DataContext.sp_mapFaults () in a try-catch so see if some execptions are occurring

like image 129
Pleun Avatar answered Sep 18 '22 23:09

Pleun


You can fire up SQL Server Profiler to see the parameters that are getting passed into the procedure (assuming there are parameters) and see if that is what is hanging up.

Update

Ensure that Settings.Default.SQLSpatialEnabled is true?

Try taking out all the other code, and just see if the SP runs on its own?

Try wrapping the call in try...catch like you have in the loop. Perhaps something is getting thrown and not reported?

like image 26
Jay Avatar answered Sep 18 '22 23:09

Jay