我需要使用存储过程填充datagrid。使用asp.net我做到了
http://g1204.hizliresim.com/w/5/43qb1.jpg
但是使用Silverlight我不能这样做。
http://e1203.hizliresim.com/v/x/3x3dw.jpg
以下是代码如何用asp.net填充datagrid:
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 @DT1=@D1
SET @DT2=@D2
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 ScoreDate=@Dt1
OPEN CRS_ALLOTMENT
FETCH NEXT FROM CRS_ALLOTMENT INTO @Name ,@Surname ,@EID ,@DayScore
WHILE @@FETCH_STATUS =0
BEGIN
IF (Select Count(*) FROM #ScoreList Where ID=@EID ) > 0
BEGIN
SET @Query=N'UPDATE #ScoreList Set F'+@FName+'='''+@DayScore+''' Where ID='+ Convert(varchar(30),@EID)
END
ELSE
BEGIN
SET @Query=N'INSERT INTO #ScoreList (Name,Surname,ID,F'+@FName+') Values('''+@Name+''' '+','+' '''+ @SurName+ ' '''+ ','+Convert(Varchar(25),@EID)+','+' '''+@DayScore+' '''+') '
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中的存储过程中填充datagrid?
它应该在域服务还是在服务器端?如果你写一个例子会很棒。
非常感谢
答案 0 :(得分:0)
只需考虑一下代码运行的位置和方式:
,您的代码在Silverlight插件内的客户端上运行。那里没有数据库服务器 - 很可能,客户端机器也无法从他的位置直接连接到数据库服务器。
因此,在Silverlight中,您需要通过域服务进行所有数据访问。您的域服务需要公开一种方法来获取该数据并以某种方式返回它,以便SL客户端可以在客户端的计算机上显示它