[英]How to get value from stored procedure which returns dynamic table in Asp mvc EDMX
I am using stored procedure in edmx. 我在edmx中使用存储过程。 I am using following SP.
我正在使用以下SP。
Create getreportDAta(@Reportname varchar(50),@startDate datetime,@enddate datetime)
begin
IF OBJECT_ID('tempdb..#ouputtable ') IS NOT NULL DROP TABLE #ouputtable ;
create #ouputtable (usdate datetime)
if(@reportname="abc")
begin
alter #ouputtable add(Some columns)
end
else begin
alter #ouputtable add(Some columns)
End
so oonn...
Select * from #ouputtable ;
Now i want to read the value selected by Select * from #ouputtable in edmx . 现在我想读取edmx中的#ouputtable中的Select *选择的值。 ouputtable is not contain fixed number of columns.
可输出表不包含固定数量的列。
How can i do this in ASP mvc EDMX. 如何在ASP MVC EDMX中执行此操作。
Basically I wanted to do the same thing, but in the end couldn't figure it out... so i decided on creating a class that i could call instead when wanting to call sp's like this. 基本上我想做同样的事情,但最终还是无法解决……所以我决定创建一个可以像这样调用sp时可以调用的类。 This link gives the basics on how to call and bind the sp's results to your Gridview: http://www.aspnettutorials.com/tutorials/database/db-storedprocs-aspnet2-csharp.aspx
该链接提供了有关如何调用sp的结果并将其绑定到Gridview的基础知识: http : //www.aspnettutorials.com/tutorials/database/db-storedprocs-aspnet2-csharp.aspx
(see the "The flow for the code behind page is as follows." section. Also, they have a link to a VB.NET version on that page if you're not into C#) (请参阅“页面后面的代码流如下。”部分。此外,如果您不使用C#,它们也具有指向该页面上VB.NET版本的链接)
Here's a modified version of my code-behind button click event: 这是我的代码隐藏按钮单击事件的修改版本:
try {
System.Configuration.Configuration rootWebConfig =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebsite"); //create connection string object
System.Configuration.ConnectionStringSettings connString;
if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0) {
connString = rootWebConfig.ConnectionStrings.ConnectionStrings["MyConnectionString"];
if (connString != null) {
System.Data.SqlClient.SqlCommand cmd =
new System.Data.SqlClient.SqlCommand("MyStoredProcedure", new System.Data.SqlClient.SqlConnection(connString.ConnectionString));
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Connection.Open();
GridViewResults.DataSource = cmd.ExecuteReader();
GridViewResults.DataBind();
cmd.Connection.Close();
cmd.Connection.Dispose();
}
else {
throw new Exception("No MyConnectionString Connection String found in web.config.");
}
}
else {
throw new Exception("No Connection Strings found in web.config.");
}
}
catch (Exception) {
throw;
}
Simply incorporate that into a class of your own in your datalayer project where your edmx file is and use it to call such stored procedures. 只需将其合并到edmx文件所在的数据层项目中您自己的类中,然后使用它来调用此类存储过程。 :).
:)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.