如何使用C#中的sqlite max()函数? [英] How do I use the sqlite max() function from C#?
问题描述
我正在使用C#/ WPF应用程序中的SQLite数据库。我有一个表有一个声明为 INTEGER UNIQUE 的列。
我从通常的<$ c执行下面的SQL脚本$ c> ExecuteReader()代码,我得到一个结果,这是我所期望的。问题是结果总是为零。有谁知道我可能做错了什么?
I am using an SQLite database from a C#/WPF application. I have a table that has a column that is declared as INTEGER UNIQUE.
I execute the SQL script below from my usual ExecuteReader()
code and I get one single result, which is what I would expect. The problem is that the result is always zero. Does anyone know what I may be doing wrong?
SELECT max( enquiry_job_number ) FROM EnquiryJobs;
我尝试过的:
查看调试器,看来该列不存在。即使我向SQL添加 AS 子句(如下所示),命名列 SQLiteDataReader.GetOrdinal(columnName)
也会返回-1。 />
What I have tried:
Looking in the debugger, it appears that the column does not exist. Even if I add an AS clause to the SQL (shown below), the named column SQLiteDataReader.GetOrdinal( columnName )
returns -1.
SELECT max( enquiry_job_number ) FROM EnquiryJobs AS "ABC";
推荐答案
除了RyanDev的出色建议外,还值得注意的是,如果你使用这个SQL:
In addition to RyanDev's excellent suggestion, it's also worth noting that if you use this SQL:
SELECT max( enquiry_job_number ) FROM EnquiryJobs;
你无法从这样的DataReader访问数据:
You cannot access the data from a DataReader like this:
SQLiteDataReader.GetOrdinal( columnName )
Becaus eyou haven;'给数据列一个名称。
将来,当你返回时尝试命名列:
Becaus eyou haven;'t given the data column a name at all.
In future, try naming the column when you return it:
SELECT max( enquiry_job_number ) AS MaxJobNo FROM EnquiryJobs;
您可以按名称访问该列:
And you can access the column by name:
int max = (int) myDataReader["MaxJobNo"];
正如评论中所提到的那样使用ExecuteScalar,因为它返回第一行的第一列。
使用DataReader参见解决方案1。
As mentioned in comments use ExecuteScalar instead since it returns the first column of the first row.
And to use DataReader see Solution 1.
这篇关于如何使用C#中的sqlite max()函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!