2012-04-05 171 views
1

我需要用存儲過程填充數據網格。隨着asp.net我做到了使用存儲過程填充DataGrid(Silverlight業務應用程序)

http://g1204.hizliresim.com/w/5/43qb1.jpg

,但使用Silverlight,我不能這樣做。

http://e1203.hizliresim.com/v/x/3x3dw.jpg

這裏是我可以填寫在DataGrid中asp.net代碼:

DataTable dt = new DataTable(); 
SqlCommand cmd = new SqlCommand(); 

// Open the connection 
using (SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString)) 
{ 
    cnn.Open(); 

    cmd.Connection = cnn; 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.CommandText = "sp_get_Scorelist"; 

    cmd.Parameters.Add("d1", new DateTime(2012, 03, 01)); 
    cmd.Parameters.Add("d2", DateTime.Now); 

    using (SqlDataAdapter da = new SqlDataAdapter(cmd)) 
    { 
     da.Fill(dt); 
    } 
}   

GridView1.DataSource = dt.DefaultView; 
GridView1.DataBind(); 

這裏是我的存儲過程;

USE [ATLAS] 
GO 
/****** Object: StoredProcedure [dbo].[sp_get_Scorelist] Script Date: 05.04.2012 14:54:30 ******/ 
SET ANSI_NULLS ON 
GO 
SET QUOTED_IDENTIFIER ON 
GO 
ALTER Procedure [dbo].[sp_get_Scorelist](@d1 datetime,@d2 datetime) 
AS 
BEGIN 
SET FMTONLY OFF --SQL 2008 sonrası desteği yok, bir sonraki sürümde problem çıkacak 
SET NOCOUNT ON 
DECLARE @DT1 DateTime ,@DT2 DateTime ,@ToDay DateTime 
DECLARE @Name nvarchar(25), @Surname nvarchar(25) , @EID int, @DayScore nvarchar(25) 
DECLARE @Query varchar(Max), @FName varchar(80) 
if (select name from tempdb..sysobjects where name like '#ScoreList%') <> '' DROP TABLE #Scorelist 
Create Table #ScoreList (Name nvarchar(25),Surname nvarchar(25),ID int) 
SET @[email protected] 
SET @[email protected] 
WHILE @DT1 <= @DT2 
BEGIN 
    SET @FName = Convert (Varchar(2),DATEPART(day,@DT1))+'_'+Convert(Varchar(2),DATEPART(month,@DT1)) -- +Convert(Varchar(4),DATEPART(Year,@DT1)) 
    SET @Query=N'ALTER TABLE #ScoreList ADD F'+ @FName +' nvarchar(25)' 
    EXECUTE(@Query) 
    DECLARE CRS_ALLOTMENT CURSOR FOR 
    --SET DATEFIRST 1 
    Select hr.Name, hr.Surname, hr.EmployeeID, sc.DayScore from hr_scoring sc left join hr_employee hr on sc.EmployeeID = hr.employeeID Where [email protected] 
OPEN CRS_ALLOTMENT 
FETCH NEXT FROM CRS_ALLOTMENT INTO @Name ,@Surname ,@EID ,@DayScore 
WHILE @@FETCH_STATUS =0 
BEGIN   
    IF (Select Count(*) FROM #ScoreList Where [email protected]) > 0 
    BEGIN 
    SET @Query=N'UPDATE #ScoreList Set F'[email protected]+'='''[email protected]+''' Where ID='+ Convert(varchar(30),@EID) 
    END 
    ELSE 
    BEGIN 
SET @Query=N'INSERT INTO #ScoreList (Name,Surname,ID,F'[email protected]+') Values('''[email protected]+''' '+','+' '''+ @SurName+ ' '''+ ','+Convert(Varchar(25),@EID)+','+' '''[email protected]+' '''+') ' 
    END 
     EXECUTE(@Query)   
FETCH NEXT FROM CRS_ALLOTMENT INTO @Name ,@Surname ,@EID ,@DayScore 
END 
    CLOSE CRS_ALLOTMENT 
    DEALLOCATE CRS_ALLOTMENT  
    SET @DT1 = @DT1+1 
END 


Select * From #Scorelist Order By Name,SurName 

END 

我的問題是如何從Silverlight中的存儲過程填充數據網格?

它應該在域服務還是在服務器端?如果你寫一個例子會很棒。

還是要謝謝你

+0

請查看此鏈接(http://weblogs.asp.net /scottgu/archive/2007/08/16/linq-to-sql-part-6-retrieving-data-using-stored-procedures.aspx) – Hukam 2012-04-05 12:10:54

+0

好得多,但你應該編輯你的[上一個問題](http: //stackoverflow.com/questions/9956931/fill-silverlight-datagrid-with-stored-procedure-entity-framework) – 2012-04-07 23:55:42

回答

0

試想想,在哪裏以及如何您的代碼運行一分鐘:

  • ASP.NET,一切都是(網絡)上運行的服務器和變成HTML,發送到客戶端以顯示在瀏覽器中。您的代碼完全服務器(除了JavaScript腳本的)上運行 - 因此可以直接訪問數據庫服務器和存儲過程

  • 的Silverlight,另一方面,你的代碼的運行客戶端在Silverlight插件中。這裏沒有數據庫服務器 - 很可能,客戶機也不能從他的位置直接連接到數據庫服務器。

因此,在Silverlight中,您需要通過域服務進行所有數據訪問。您的域名服務需要公開一種獲取該數​​據的方法,並以某種方式將其返回,SL客戶端可以將其顯示在客戶端計算機上

相關問題