如何使用executioncalar获取记录计数 [英] How to get record count using executescalar

查看:161
本文介绍了如何使用executioncalar获取记录计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,但不起作用: -

this is my code but not work:-

void CountTotal()
        {
            con.Open();
            s = "select Count(*) from ResrvationData where Date_Reservation = @Date_Reservation";
            sCommand = new SqlCommand(s , con);
            sCommand.Parameters.AddWithValue("@Date_Reservation", dateTimePicker_Time.Text);
            object count = sCommand.ExecuteScalar();
            if (count != null) txt_TotalBooks.Text = count.ToString();
            con.Close();
        }

private void FRM_Reservation_date_Load(object sender, EventArgs e)
        {
           CountTotal();
        }





我的尝试:



i希望显示计数数据



What I have tried:

i want display count where data

推荐答案

当我更改日期时,计数未在文本框中更改



嗯 - 那是因为你没有告诉它改变。



你显示的代码调用了CountTotal方法表单加载事件 - 而不是其他地方。这意味着SQL只执行一次:首次加载表单时。您通过DateTimePicker进行的任何更改都不会自动更新计数。

您可以尝试在DateTimePicker.ValueChanged事件(System.Windows.Forms) [ ^ ]处理程序。



但是请不要使用DTP的Text属性来检查数据库 - 使用已经是DateTime的Value属性,所以没有机会SQL和您的应用程序之间的沟通错误与数据格式有关!
when i change date , count not changed in textbox

Well - that's because you haven't told it to change.

The code you show calls the CountTotal method in the form Load event - and nowhere else. Which means that the SQL is executed only once: when the form is first loaded. Any changes you make via the DateTimePicker do not automatically update the count at all.
You could try calling the method in your DateTimePicker.ValueChanged Event (System.Windows.Forms)[^] handler.

But please, don't use the Text property of the DTP to check the DB - use the Value property which is already a DateTime directly, so there is no chance of miscommunication between SQL and your app as concerns data formats!


这篇关于如何使用executioncalar获取记录计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆