1

我的过程返回 25 行。我DataContext在调试模式下运行时看到了它。但是在数据绑定后,gridview 没有显示任何内容。当我将select条款更改为

Select * 
from pgnchess 
order by GameID 
OFFSET @Offset ROW 
FETCH NEXT @PageSize ROW ONLY

一切都好。当我在没有任何参数的 SQL Server Management Studio 中执行过程时,也运行得很好。

这是我的 SQL Server 过程

CREATE PROCEDURE [dbo].[GetGamesWithPlayer](
    @qText nvarchar(40) = '%',
    @qColumn nvarchar(10) = 'BlackWhite',
    @orderColumn nvarchar(20) = 'GameID',
    @orderDirection bit = 1,
    @PageSize int = 25,
    @CurrentPage int = 0)
AS
BEGIN
    DECLARE @Offset int;

    SET NOCOUNT ON;
    SET @Offset = @PageSize * @CurrentPage

    SELECT 
        [GameID], [Event], [Site], [Date], [White], [Black], [Result], [ECO] 
    FROM 
        [dbo].[pgnGames]  
    WHERE 
         CASE @qColumn
             WHEN 'BlackWhite' THEN [Black]
             WHEN 'Event' THEN [Event]
             WHEN 'Black' THEN [Black]
             WHEN 'White' THEN [White]
             WHEN 'ECO' THEN  [ECO] 
             WHEN 'Result' THEN [Result] 
          END LIKE  '%'+ @qText +'%' OR
          CASE @qColumn
             WHEN 'BlackWhite' THEN [White]
             WHEN 'Event' THEN [Event]
             WHEN 'Black' THEN [Black]
             WHEN 'White' THEN [White]
             WHEN 'ECO' THEN  [ECO] 
             WHEN 'Result' THEN [Result] 
          END LIKE  '%'+ @qText +'%'
       ORDER BY 
          CASE WHEN @orderColumn = 'Black' 
                    AND @orderDirection = 0 THEN [Black] END DESC,
          CASE WHEN @orderColumn = 'Black' 
                    AND @orderDirection = 1 THEN [Black] END,
          CASE WHEN @orderColumn = 'Date' 
                    AND @orderDirection = 0 THEN [Date] END DESC,
          CASE WHEN @orderColumn = 'Date'  
                    AND @orderDirection = 1 THEN [Date] END,
          CASE WHEN @orderColumn = 'ECO' AND @orderDirection = 0 THEN [ECO] END DESC,
          CASE WHEN @orderColumn = 'ECO' AND @orderDirection = 1 THEN [ECO] END,
          CASE WHEN @orderColumn = 'Event' AND @orderDirection = 0 THEN [Event] END DESC,
          CASE WHEN @orderColumn = 'Event' AND @orderDirection = 1 THEN [Event] END,
          CASE WHEN @orderColumn = 'GameID' AND @orderDirection = 0 THEN [GameID] END DESC,
          CASE WHEN @orderColumn = 'GameID' AND @orderDirection = 1 THEN [GameID] END,
          CASE WHEN @orderColumn = 'Result' AND @orderDirection = 0 THEN [Result] END DESC,
          CASE WHEN @orderColumn = 'Result' AND @orderDirection = 1 THEN [Result] END,
          CASE WHEN @orderColumn = 'Site' AND @orderDirection = 0 THEN [Site] END DESC,
          CASE WHEN @orderColumn = 'Site' AND @orderDirection = 1 THEN [Site] END,
          CASE WHEN @orderColumn = 'White' AND @orderDirection = 0 THEN [White] END DESC,
          CASE WHEN @orderColumn = 'White' AND @orderDirection = 1 THEN [White] END
          OFFSET @Offset ROW 
          FETCH NEXT @PageSize ROW ONLY
END
GO

这是我的绑定代码:

DateTime startTime = DateTime.Now;
pgDataContext pl = new pgDataContext();
GameList.DataSource = pl.GetGamesWithPlayer(hdr.qText,hdr.qColumn,GlobalVar.sortColumn,GlobalVar.sortOrder, GlobalVar.pageSize, pgIndex);
GameList.DataBind();
recordCount = pl.pgnGames.Count();
GameList.VirtualItemCount = recordCount;
DateTime endTime = DateTime.Now;
TimeSpan span = endTime.Subtract(startTime);
4

1 回答 1

0

如果您将 pl.GetGamesWithPlayer 结果存储到某个 var 属性中并设置断点,您是否在调用后看到该值中的数据?另外, pgnGames.Count() 是否给您正确的数字?看起来您的代码应该是:

DateTime startTime = DateTime.Now;
pgDataContext pl = new pgDataContext();
var games = pl.GetGamesWithPlayer(hdr.qText,hdr.qColumn,GlobalVar.sortColumn,GlobalVar.sortOrder, GlobalVar.pageSize, pgIndex);
GameList.DataSource = games;
GameList.DataBind();
recordCount = pl.pgnGames.Count();
GameList.VirtualItemCount = recordCount;
DateTime endTime = DateTime.Now;
TimeSpan span = endTime.Subtract(startTime);

但我不确定您的数据访问层是如何工作的。这假定 pl.GetGamesWithPlayer 返回 pgnGames 对象。如果该方法在调用时仅填充 pl.pgnGames,那么您只需执行以下操作:

DateTime startTime = DateTime.Now;
pgDataContext pl = new pgDataContext();
pl.GetGamesWithPlayer(hdr.qText,hdr.qColumn,GlobalVar.sortColumn,GlobalVar.sortOrder, GlobalVar.pageSize, pgIndex);
GameList.DataSource = pl.pgnGames;
GameList.DataBind();
recordCount = pl.pgnGames.Count();
GameList.VirtualItemCount = recordCount;
DateTime endTime = DateTime.Now;
TimeSpan span = endTime.Subtract(startTime);

如果您确定正确的数据正在进入数据源,那么我建议您放入一些网格事件,例如 DataBinding、RowDataBound 等,然后逐步查看是否可以判断为什么数据不是可用的。

于 2013-08-09T18:26:20.213 回答