2012-10-19 39 views
0

我正在EDMX中使用存儲過程。我正在使用以下SP。如何從存儲過程中獲得值返回動態表在Asp mvc EDMX

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 ; 

現在我想讀取選擇*從edmx #ouputtable中選擇的值。輸出不包含固定的列數。

我如何在ASP mvc EDMX中做到這一點。

回答

0

基本上我想做同樣的事情,但最終無法弄清楚......所以我決定創建一個類,我可以打電話,而不是想打電話給這樣的sp。 此鏈接提供了有關如何調用和SP的結果綁定到你的GridView的基本知識: http://www.aspnettutorials.com/tutorials/database/db-storedprocs-aspnet2-csharp.aspx

(參見「爲代碼隱藏頁的流程如下。」部分此外,他們有一個鏈接。該頁面如果你不進入C#)

這裏在VB.NET版本是我的代碼隱藏按鈕單擊事件的修改版本:

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; 
} 

簡單地納入到這一點在一類自己的您的edmx文件所在的數據層項目,並使用它來調用此類存儲過程。 :)。