1

此处可能重复

有人知道找出 SqlDataSource 是否会返回零行的好方法吗?

这是我在后面的 C# 代码中对 SQL 数据库的调用:

        string SQL_PROD_DOCS =
            @"SELECT   TOP 3  Documents.ProductID, Documents.ProjectID, Documents.DocTitle, Documents.DocURL
              FROM         Products INNER JOIN
                  Documents ON Products.ID = Documents.ProductID
              WHERE     (Products.ShowOnScorecard = 1) AND (Products.Deleted = 0) AND (Products.ID = 15) AND (Documents.ProjectID IS NULL) AND (Documents.Deleted = 0) AND (Documents.AttributeID = 1)";


        SqlDataSource dsProdDocsHeader = new SqlDataSource(utils.Conn(Server.MachineName), SQL_PROD_DOCS);
        RptrProductDocs.DataSource = dsProdDocsHeader.Select(DataSourceSelectArguments.Empty);
        RptrProductDocs.DataBind();

我想知道 dsProdDocsHeader 是否会返回零,以便我可以返回不同的消息。
谢谢!

4

3 回答 3

2

尝试这个。

    SqlDataSource dsProdDocsHeader = new SqlDataSource(utils.Conn(Server.MachineName), SQL_PROD_DOCS);
    DataView dvsqllist = (DataView)dsProdDocsHeader.Select(DataSourceSelectArguments.Empty);
    DataTable tablelist = dvsqllist.Table;
    int n = tablelist.Rows.Count;
    if (n > 0)
    {
       RptrProductDocs.DataSource = dsProdDocsHeader.Select(DataSourceSelectArguments.Empty);
       RptrProductDocs.DataBind();
    }
    else
    {
       // whatever u want to show.
    }
于 2013-08-09T17:42:44.937 回答
1

select 方法返回一个 DataView 对象,您可以检查 Count 属性以查看是否返回了任何记录:

SqlDataSource dsProdDocsHeader = new SqlDataSource(utils.Conn(Server.MachineName), SQL_PROD_DOCS);
DataView view = (DataView)dsProdDocsHeader.Select(DataSourceSelectArguments.Empty);

    if (view.Count == 0)
    {
        lblMessage.Text = "Message!!!";
    }
    else
    {
        RptrProductDocs.DataSource = view;
        RptrProductDocs.DataBind();
    }
于 2013-08-09T18:28:19.627 回答
1

中继器没有 EmptyData 模板(假设 Rptr=repeater)。

所以看看这里: http ://devcenter.auburnrandall.com/EmptyRepeaterData.aspx

于 2013-08-09T17:37:20.260 回答