Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate Reusable Linq To SQL For Stored Procedures
    primarykey
    data
    text
    <p>I am working on a new project that needs to use <code>Linq To SQL</code>. I have been asked to create a generic or reusable <code>Linq to SQL</code> class that can be used to execute stored procedures. </p> <p>In <code>ADO.Net</code> 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:</p> <pre><code>SqlCommand cmd = new SqlCommand("myStoredProc", conn); // etc, etc </code></pre> <p>I am struggling with how to create something similar in <code>Linq To SQL</code>, if it is even possible. I have created a <code>.dbml</code> file and added my stored procedure to it. As a result, I can return the results using the code below:</p> <pre><code>public List&lt;myResultsStoreProc&gt; GetData(string connectName) { MyDataContext db = new MyDataContext (GetConnectionString(connectName)); var query = db.myResultsStoreProc(); return query.ToList(); } </code></pre> <p>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.</p> <p>So is there a way to create a reusable <code>Linq to SQL</code> class to execute stored procs? </p> <p>Edit:</p> <p>What I am looking for is if there is a way to do something like the following?</p> <pre><code>public List&lt;string&gt; GetData(string connectName, string procedureName) { MyDataContext db = new MyDataContext (GetConnectionString(connectName)); var query = db.procedureName(); return query.ToList(); } </code></pre> <p>I have reviewed the <a href="http://msdn.microsoft.com/en-us/library/bb425822.aspx#linqtosql_topic29">MSDN docs</a> on <code>Linq To Sql</code> and these are showing the table in the <code>IEnumerable</code>:</p> <pre><code>IEnumerable&lt;Customer&gt; results = db.ExecuteQuery&lt;Customer&gt;( @"select c1.custid as CustomerID, c2.custName as ContactName from customer1 as c1, customer2 as c2 where c1.custid = c2.custid" ); </code></pre> <p>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 <code>Linq To Sql</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload