我有一个gridview,它由一个存储的proc通过标准的n层结构提供,它应该触发emplty行对象,但它没有。相反,我得到一个例外“IListSource不包含任何数据源”。如果我在SQL Server Management Studion中运行查询,则返回一个空行。
我怀疑问题出在transact-sql上,我使用的是PIVOR consrtuct,因为我不熟悉它是如何工作的,我认为这会导致问题。它是从this blog借来的,并由我的前任修改过。
有谁知道导致问题的原因是什么?这里的代码。
<asp:GridView ID="gvSystemRMRiskRpt" runat="server" AutoGenerateColumns="true"
CellPadding="3" PageSize="25" BackColor="White" BorderColor="MidnightBlue"
BorderStyle="Groove" BorderWidth="1px" CssClass="TextCompact" GridLines="Vertical"
OnRowDataBound="gvSystemRMRiskRpt_OnDataBound" EmptyDataText="Your request has returned zero records" >
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="Gainsboro" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" VerticalAlign="Top" />
</asp:GridView>
代码文件:
protected void btnSearch_Click(object sender, EventArgs e)
{
Guid site = new Guid(ddlSites.SelectedValue);
int interval = Convert.ToInt16(ddlIntervals.SelectedValue);
string[] startDate = tbStartDate.Text.Split('/'); // 01-2001
string[] stopDate = tbEndDate.Text.Split('/'); // 12/2002
gvSystemRMRiskRpt.DataSource = Data.ByMonthYear(connectionManager,site,startDate[1],stopDate[1],interval);
gvSystemRMRiskRpt.DataBind();
}
数据层:
public static DataSet ByMonthYear(ConnectionManager connectionManager, Guid site, string startDate, string stopDate, int interval)
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "[dbo].[ByMonthYear_StoredProc]";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = connectionManager.GetSqlConnectionContext();
cmd.Transaction = connectionManager.GetSqlTransactionContext();
SqlParameter paramSiteGUID = new SqlParameter("@SiteGUID", SqlDbType.UniqueIdentifier);
paramSiteGUID.Value = site;
cmd.Parameters.Add(paramSiteGUID);
SqlParameter paramStartDate = new SqlParameter("@StartDate", SqlDbType.VarChar);
paramStartDate.Value = startDate;
cmd.Parameters.Add(paramStartDate);
SqlParameter paramStopDate = new SqlParameter("@StopDate", SqlDbType.VarChar);
paramStopDate.Value = stopDate;
cmd.Parameters.Add(paramStopDate);
SqlParameter paramProcedureAction = new SqlParameter("@ProcedureAction", SqlDbType.Int);
paramProcedureAction.Value = 0; // From Primary Key
cmd.Parameters.Add(paramProcedureAction);
SqlParameter paramInterval = new SqlParameter("@Interval", SqlDbType.Int);
paramInterval.Value = interval;
cmd.Parameters.Add(paramInterval);
SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sqlDA.Fill(ds);
cmd.Dispose();
cmd = null;
return ds;
}
和实际存储过程:
alter PROCEDURE [dbo].[ByMonthYear_StoredProc]
@ProcedureAction int,
@SiteGUID uniqueidentifier = null,
@StartDate varchar(4),
@StopDate varchar(4),
@Interval int
AS
DECLARE @cols NVARCHAR(2000)
DECLARE @query NVARCHAR(4000)
IF (@ProcedureAction = 0) -- From
BEGIN
SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED
IF (@Interval = 1) -- Montly
BEGIN
SELECT @cols = STUFF(( SELECT DISTINCT TOP 100 PERCENT '],[' + cast(MonthYear as varchar(10)) FROM vByMonthYear where SiteGUID = @SiteGuid
and [Year] BETWEEN '2005' AND '2010' ORDER BY '],[' + cast(MonthYear as varchar(10)) FOR XML PATH('') ), 1, 2, '')
+ ']' SET @query = N'SELECT EventType, ' + @cols +' FROM (SELECT MonthYear,EventType,Value,OrderBy FROM vRByMonthYear
where SiteGUID = ' + CHAR(39) + CONVERT(nvarchar(36), @SiteGuid) + CHAR(39) + ' and Year BETWEEN 2005 AND 2010) p
PIVOT ( Sum ([Value] ) FOR MonthYear IN ( '+ @cols +' ) ) AS pvt order by OrderBy'
execute(@query)
END
END
重申一下,如果我运行SELECT DISTINCT TOP 100 PERCENT'],['+ cast(MonthYear as varchar(10))FROM vByMonthYear,其中SiteGUID = @SiteGuid,[Year] BETWEEN'2005'和'2010'in查询窗口,我用@SiteGuid替换一个我知道没有记录的实际值我得到一个空行但在我的应用程序中我得到了异常。
答案 0 :(得分:0)
我怀疑这是发生了什么,但并非100%肯定。适配器
SqlDataAdapter sqlDA = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sqlDA.Fill(ds);
正在填充一行没有数据,因此返回的数据集不被视为空 - 不确定原因。我怀疑奇怪的SQL Pivot正在返回一些引用或空格。
无论如何,我更改了方法以返回DataTable
而不是DataSet
,并将整个SqlDataAdapter
视为一个,而是使用了一个有效的SqlReader
。可能在此过程中节省了大约1毫秒的开销。
using (SqlDataReader _reader = cmd.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(_reader);
cmd.Dispose();
return dt;
}