首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >返回行数并赋值给Int变量的查询

返回行数并赋值给Int变量的查询
EN

Stack Overflow用户
提问于 2011-11-20 12:11:32
回答 1查看 3K关注 0票数 0

我正在选择我的查询中的行数。我需要将它赋给一个int变量。我该如何做到这一点呢?

Display.aspx.cs

代码语言:javascript
复制
            **int TotalYesVotes;**
            Ratings GetYesVotes = new Ratings();
            DataTable DATotalYesVotes = GetYesVotes.GetTotalYesVotes();
            **//How do i assign that count to TotalYesVotes**

业务层

代码语言:javascript
复制
     public DataTable GetTotalYesVotes()
     {

         DLRatings DlTotalyesVotes = new DLRatings();
         return DlTotalyesVotes.GetTotalYesVotes();

     }

我的数据层

代码语言:javascript
复制
    public DataTable GetTotalNoVotes()
    {
        //get database connection string from config file
        string strConectionString o= ConfigurationManager.AppSettings["DataBaseConnection"];

        //set up sql
        string StrSql = "SELECT COUNT(*) AS Expr1 FROM Ratings WHERE (PicRating = 2) AND (PicID = 2)";

        DataTable dt = new DataTable();
        using (SqlDataAdapter daObj = new SqlDataAdapter(StrSql, strConectionString))
        {

            //fill data table
            daObj.Fill(dt);
        }
        return dt;
        }
EN

回答 1

Stack Overflow用户

发布于 2011-11-20 12:26:32

你应该在数据检索方面做很多改变:

代码语言:javascript
复制
   public int GetTotalNoVotes()
    {
        //get database connection string from config file
        string strConectionString o= ConfigurationManager.AppSettings["DataBaseConnection"];

        SqlConnection conn = new SqlConnection(strConnectionString);
        conn.Open();

        SqlCommand oCommand = new SqlCommand("SELECT COUNT(1) AS Expr1 FROM Ratings WHERE (PicRating = 2) AND (PicID = 2)", conn);

        object oValue = oCommand.ExecuteScalar();

        conn.Close();

        if (oValue == DBNull.Value) {
           return 0;
        } else {
          return Convert.ToInt32(oValue);
        }
   }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8199386

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档