问题
I see you can specify Insert, Update and Delete stored procs, but no straightforward way for SELECT stored procs.
回答1:
This is doable but not via the visual drag / drop tool. You have to do three things:
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)); }
}
Create a new page template (List.aspx for example) for the particular table you want in the CustomPages folder to control the selecting from.
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
回答2:
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.
回答3:
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
来源:https://stackoverflow.com/questions/1059863/how-do-you-tell-asp-net-dynamic-data-to-use-stored-procedures-for-select