繁体   English   中英

如何从C#中的SQL SqlDataSource检查零行?

[英]How can I check for Zero rows from SQL SqlDataSource in C#?

可能在这里重复

有没有人知道找出SqlDataSource是否会返回Zero Rows的好方法?

这是我在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是否会返回零,所以我可以返回一个不同的消息。
谢谢!

尝试这个。

    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.
    }

转发器没有EmptyData模板(假设Rptr = repeater)。

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

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();
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM