简体   繁体   English

打开数据集,计数记录并将数字返回变量的最快方法

[英]Quickest way to open dataset, count the records and return the number to a variable

I'm pretty new to C#/WPF/SQLite and have created a form that will need to display several calculations (from two different tables). 我是C#/ WPF / SQLite的新手,并且创建了一个表单,该表单需要显示多个计算(来自两个不同的表)。 I'd like to know what you guys think the fastest way to do this would be (and if anyone could provide a rough guide for the code - it would be a big help). 我想知道你们认为最快的方法是(如果有人可以提供代码的粗略指导,那将是很大的帮助)。

It's a bit rough (and not fully working), but here it is: 这有点粗糙(并且不能完全正常工作),但是这里是:

    string cs = ClsVariables.StrDb;
    using(SQLiteConnection con = new SQLiteConnection(cs))
    {
        con.Open();
        string stm = "SELECT [ID] FROM tblActivities WHERE [Activity]='Sleeping'";
        using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
        {
            //I'm guessing this is where I need count the number of rows (but haven't figure that bit out just yet
        }
        con.Close();   
     }

The aggregate function COUNT of SQL could solve you problem, but on the C# code you don't need to fill a dataset with a table and then count the rows found. SQL的聚合函数COUNT可以解决您的问题,但是在C#代码上,您不需要用表填充数据集,然后对找到的行进行计数。 Neither you need a SQLiteDateReader because when you expect your result to be just one row with only one column you use ExecuteScalar 您都不需要SQLiteDateReader,因为当您期望结果只是一行且只有一列时,您将使用ExecuteScalar

string cmdText = "SELECT COUNT(ID) FROM tblActivities WHERE [Activity] = 'Sleeping'";
using(SQLiteConnection con = new SQLiteConnection(cs))
using(SQLiteCommand cmd = new SQLiteCommand(cmdText, con))
{
    con.Open();
    int count = Convert.ToInt32(cmd.ExecuteScalar());
    Console.WriteLine("You have " + count.ToString() + " records sleeping");
}

A last note on ExecuteScalar. 关于ExecuteScalar的最后说明。 It could return NULL but, in your case (COUNT returns zero or a number > 0), this is not possible so I don't check the return value for null and I directly convert it to an integer 它可能返回NULL,但在您的情况下(COUNT返回零或数字> 0),这是不可能的,因此我不检查返回值是否为null,而是直接将其转换为整数

You can get the count of the dataset by adding the count as a column in your query: 您可以通过在查询中将计数添加为列来获取数据集的计数:

    int count = 0;
    using(SQLiteConnection con = new SQLiteConnection(cs))
    {
        con.Open();
        string stm = "select count(id) as count from tblActivities WHERE [Activity] = 'Sleeping'";
        using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
        {
            using (IDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    if (reader["count"] != DBNull.Value && reader["count"] != null)
                    {
                        if (!int.TryParse(reader["count"], out count))
                        {
                            //failed to parse
                        }
                    }
                 }
            }
        }
        con.Close();   
     }

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

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