views:

740

answers:

4

I see you can specify Insert, Update and Delete stored procs, but no straightforward way for SELECT stored procs.

A: 

linq-to-sql is a very bad idea...

but this sproc should allow you to do what you want

create PROCEDURE [dbo].[usp_GetCompanies] (
    @in_filter nvarchar(2000)
)
AS
    declare @sql nvarchar(max)
    begin
     set @sql = '
     SELECT 
      c.id as company_id, c.name as company_name
     FROM company c
     WHERE id is not null ' + @in_filter + ' order by c.type, c.name ' 
     exec sp_executesql @sql
    end
return

sample

mson
I don't see how what you propose can be a solution. I want to know where I would specify stored procs to be used for my SELECT statements
Irwin
ok - i guess i didn't understand which part you needed. see the link in above answer
mson
i'd like to stress that just because it is possible to use linq-to-sql, doesn't mean you should use it. linq-to-sql is expressly forbidden in the environments i manage.
mson
A: 

What you're looking for I believe is not exactly possible, because entities must be mapped to a table or a view of some sort. A stored procedure is not something you can define an entity against.

However, it is certainly possible to create a mapping for a stored procedure that tells Linq2Sql to return entities when it's executed, and put a method in your DataContext class to run the sproc and get your list of entities. These entities perform the same as entities created from a regular table mapping, so calling SubmitChanges() on them would use any Insert/Update/Delete sprocs that you had created for that entity type.

Probably the best thing to do would be to look at this walkthrough by Scott Guthrie.

womp
A: 

This is doable but not via the visual drag / drop tool. You have to do three things:

  1. Create a new method from the datacontext class that will be called to "get" your data

    public partial class DatabaseDataContext {
    [Function(Name = "dbo.Contact_Get")]
    [ResultType(typeof(Contact))]
    [ResultType(typeof(int))]
    public IMultipleResults GetContacts([Parameter(Name = "PageIndex", DbType = "Int")] System.Nullable<int> pageIndex, [Parameter(Name = "PageSize", DbType = "Int")] System.Nullable<int> pageSize, [Parameter(Name = "Sort", DbType = "NVarChar(10)")] string sort, [Parameter(Name = "ContactTypeId", DbType = "Int")] System.Nullable<int> contactTypeId) {
        IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), pageIndex, pageSize, sort, contactTypeId);
        return ((IMultipleResults)(result.ReturnValue));
    }
    

    }

  2. Create a new page template (List.aspx for example) for the particular table you want in the CustomPages folder to control the selecting from.

  3. Control the gridview's selecting mechanism.

    protected void GridDataSource_Selecting(object sender, LinqDataSourceSelectEventArgs e) {
    DatabaseDataContext db = new DatabaseDataContext();
    IMultipleResults results = db.GetContacts(e.Arguments.StartRowIndex, e.Arguments.MaximumRows, e.Arguments.SortExpression, (int?)e.WhereParameters["ContactTypeId"]);
    e.Result = results.GetResult<Contact>().ToList();
    e.Arguments.TotalRowCount = results.GetResult<int>().Single<int>();
    

    }

Check out the Dynamic Data SP sample on the codeplex site for DD that shows you how to do this:

http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=14473

klabranche
this looks great. will look into it.
Irwin
A: 

how is it for nonparameterised stored procedure

You're better off posting this as a separate question, if the answer to this question didn't help you. More people are likely to see your question and answer it well if you ask a new question than if you hide a question in the answers to another question.
sth