简体   繁体   中英

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.

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?

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

Are you using a static DataContext for all queries? Have you tried using separate DataContexts for each call within the loop?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM