Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8031525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T01:07:35+00:00 2026-06-05T01:07:35+00:00

I am working on a new project that needs to use Linq To SQL

  • 0

I am working on a new project that needs to use Linq To SQL. I have been asked to create a generic or reusable Linq to SQL class that can be used to execute stored procedures.

In ADO.Net I knew how to do this by just passing in a string of what I wanted to execute and I could pass in different strings for each query I need to run:

SqlCommand cmd = new SqlCommand("myStoredProc", conn); // etc, etc

I am struggling with how to create something similar in Linq To SQL, if it is even possible. I have created a .dbml file and added my stored procedure to it. As a result, I can return the results using the code below:

public List<myResultsStoreProc> GetData(string connectName)
{
   MyDataContext db = new MyDataContext (GetConnectionString(connectName));
   var query = db.myResultsStoreProc();

   return query.ToList();
}

The code works but they want me to create one method that will return whatever stored procedure I tell it to run. I have searched online and talked to colleagues about this and have been unsuccessful in finding a way to create reusable stored proc class.

So is there a way to create a reusable Linq to SQL class to execute stored procs?

Edit:

What I am looking for is if there is a way to do something like the following?

public List<string> GetData(string connectName, string procedureName)
{
   MyDataContext db = new MyDataContext (GetConnectionString(connectName));
   var query = db.procedureName();

   return query.ToList();
}

I have reviewed the MSDN docs on Linq To Sql and these are showing the table in the IEnumerable:

IEnumerable<Customer> results = db.ExecuteQuery<Customer>(
   @"select c1.custid as CustomerID, c2.custName as ContactName
      from customer1 as c1, customer2 as c2
      where c1.custid = c2.custid"
);

I am looking for something very generic, where I can send in a string value of the stored proc that I want to execute. If this is not possible, is there any documentation on why it cannot be done this way? I need to prove why we cannot pass a string value of the name of the procedure to execute in Linq To Sql

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T01:07:37+00:00Added an answer on June 5, 2026 at 1:07 am

    DataContext.ExecuteCommand is not quite what you are looking for, as it only returns an int value. What you want instead is DataContext.ExecuteQuery, which is capable of executing a stored procedure and returning a dataset.

    I would create a partial class for your DBML in which to store this function.

    public List<T> GetDataNoParams(string procname)
    {
       var query = this.ExecuteQuery<T>("Exec " + procname);
    
       return query.ToList();
    }
    
    public List<T> GetDataParams(string procname, Object[] parameters)
    {
       var query = this.ExecuteQuery<T>("Exec " + procname, parameters);
    
       return query.ToList();
    }
    

    To call a stored procedure you would do:

    GetDataNoParams("myprocedurename");
    

    or

    GetDataParams("myotherprocedure {0}, {1}, {2}", DateTime.Now, "sometextValue", 12345);
    

    or

    GetDataParams("myotherprocedure var1={0}, var2={1}, var3={2}", DateTime.Now, "sometextValue", 12345);
    

    If you want to call procedures with no return value that is easy enough too, as I’m sure you can see, by creating a new method that doesn’t store/return anything.

    The inspiration came from http://msdn.microsoft.com/en-us/library/bb361109(v=vs.90).aspx.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on creating a new C# project that needs to interact with
I have a normal VS2008 project that I've been working on for awhile that
I'm working on a project that uses the new CSS3 transform:rotate(180deg) feature. Every modern
I am new webservers...and... I am working on a django project that is ready
I'm working on a new project where I want to use Phil Haack Areas
I'm working on a new project and for some reason decided to create two
I'm working on a new project with a full ANT build. I use eclipse
I'm coming from a MS SQL Server background. Working on a new project using
I have been looking at making the switch from SQL TableAdapters to Linq using
I have a project that needs to access a DLL with PHP. The server

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.