简体   繁体   English

如何使用存储过程从 ASP.NET 记录计数?

[英]How to record count from ASP.NET using stored procedure?

Can you help me?你能帮助我吗? I am trying to output the number of depositors which has the total of 5 but when I run the program it outputs only 1. I used stored procedure for this matter.我正在尝试 output 的存款人数量,总共有 5 个,但是当我运行程序时它只输出 1 个。我为此使用了存储过程。 I think something is missing when in the codes below and I don't know.我认为在下面的代码中缺少某些东西,我不知道。 I tried my best to search in the internet.我尽力在互联网上搜索。

This code is in C#此代码在 C#

protected void Page_Load(object sender, EventArgs e) { protected void Page_Load(object sender, EventArgs e) {

        if (!IsPostBack)
        {
            CountAllUsers();
      
        }

      

    }


protected void CountAllUsers()
    {
        using (MySqlConnection mycon = new MySqlConnection(constring))
        {
            
            mycon.Open();
            MySqlCommand cmd = new MySqlCommand("countallusers", mycon);
            cmd.CommandType = CommandType.StoredProcedure;
            MySqlDataAdapter adx = new MySqlDataAdapter(cmd);
            DataSet ds= new DataSet();
            adx.Fill(ds);

            mycon.Close();

            lblDepositors.Text = ds.Tables[0].Rows.Count.ToString();
        }

You can use ExecuteScalar to do the query for counting the number of records.您可以使用ExecuteScalar进行查询以计算记录数。 Replace TABLE_NAME with your table.TABLE_NAME替换为您的表。

FYI, you don't have to call mycon.Close();仅供参考,您不必调用mycon.Close(); , as you apply using statement, when it ends, it will dispose the connection automatically. ,当您应用 using 语句时,当它结束时,它将自动处理连接。

Updated : Added using block for MySqlCommand for Disposable best practice as suggested by @Dai.更新:按照@Dai 的建议,为 MySqlCommand 添加了使用块以实现Disposable最佳实践。

protected void CountAllUsers()
{
    using (MySqlConnection mycon = new MySqlConnection(constring))
    {  
        using (MySqlCommand cmd = new MySqlCommand("SELECT COUNT(*) FROM TABLE_NAME", mycon)) 
        {
            cmd.CommandType = CommandType.Text;
            mycon.Open();

            var count = Convert.ToInt32(cmd.ExecuteScalar());
            lblDepositors.Text = count.ToString();
        }       
    }
}

MySqlCommand.ExecuteScalar Method MySqlCommand.ExecuteScalar 方法

ExecuteScalar method to retrieve a single value (for example, an aggregate value) from a database. ExecuteScalar 方法从数据库中检索单个值(例如,聚合值)。

I already solved.我已经解决了。 Thanks!谢谢!

protected void CountAllUsers()
    {
        using (MySqlConnection mycon = new MySqlConnection(constring))
        {
            
            mycon.Open();
            MySqlCommand cmd = new MySqlCommand("SELECT COUNT(id) as count from depositors_tbl", mycon);
            MySqlDataAdapter ada = new MySqlDataAdapter();
            DataSet dt = new DataSet();
            ada.SelectCommand = cmd;
            ada.Fill(dt);
            mycon.Close();

            lblDepositors.Text = dt.Tables[0].Rows[0]["count"].ToString();
        }

    }

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

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