0

I'm trying to get visual Studio to successfully call my stored procedure, which will clear a table. I'm tackling this without the use of a .dbml file and seem to be using regular Linq instead of Linq to SQL.

So far I have tried using the context.executeCommand options and other various fixes that have come up on YouTube or searching. I've decided to simply make a stored procedure in SQL Server and call it in Visual Studio. So far I've refreshed the stored procedures both in database and in Visual Studio's server view and have confirmed that Visual Studio knows that my Stored Procedure exists, it's just actually calling it that's the tough part.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;   


namespace namespace1
{
    class Program
{


    static void Main(string[] args)
    {          

        using (var db = new Model2())
        {             

            // This is the name of the stored procedure so I'm just 
            // trying to call it. I've also tried putting it inside a 
            // variable to see if that would work. Same problem.

            db.sp_clearBTEligRpt();
            // Error: Model2 does not contain a definition for
            // "sp_clearBTELigRpt" and no extension method 
            // "sp_clearBTEligRpt" accepting a first argument of type
            // 'Model2' could be found
        }

The error that I get is posted in the comments on the code and as far as expected results go I want it to work and execute the stored procedure called.

  • What do you mean by: "confirmed that Visual Studio knows that my Stored Procedure exists"? Do you mean it's showing up in the server browser? If so, that doesn't mean that your application knows about the stored proc, it just means that the server browser can see it. I think you might want to look into Entity Framework, and create an entity model. This is basically an automatic mapping between your database objects and your code, and would allow you to call your stored proc in the way you are trying to. – Jonathan Feb 12 '19 at 21:29
  • Yes, it shows up in the server explorer in Vis Studio – sirscoobs Feb 12 '19 at 21:56
  • Yeah - exactly - it's not your project that 'knows' about the stored proc. Read my comment and I recommend looking into Entity Framework – Jonathan Feb 12 '19 at 21:59
  • what's interesting here is that I was able to run queries and such without building an entity framework simply using the model database so my question here would be if there was a method that would allow for calling a stored procedure without putting an entire entity framework into place for one procedure. I appreciate you taking the time to help me, I'm relatively green at this so thanks. – sirscoobs Feb 12 '19 at 22:11
  • Yes, you can execute a stored procedure without entity framework. See: https://stackoverflow.com/questions/1260952/how-to-execute-a-stored-procedure-within-c-sharp-program But if you want to call it the way you have noted, and get the response back in a structured object, etc etc. you'll find Entity Framework is the easier way to go. Even for your simple queries; you won't have to build up text queries to pass through, you'll be able to do it all programmatically, with strongly-typed components, syntax checking, etc. – Jonathan Feb 13 '19 at 15:58
  • Thanks so much for your help man, it seems like I've pieced together something that works – sirscoobs Feb 15 '19 at 17:28

0 Answers0