Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从NpgsqlDataReader获取查询结果_C#_Npgsql_Dbdatareader - Fatal编程技术网

C# 从NpgsqlDataReader获取查询结果

C# 从NpgsqlDataReader获取查询结果,c#,npgsql,dbdatareader,C#,Npgsql,Dbdatareader,这是我的功能: public int gtCountCertificatByExercice() { DataTable resDataTable = new DataTable(); DBConnection dbConnection = new DBConnection(); string query = "SELECT COUNT(id) AS nb_cert " +

这是我的功能:

public int gtCountCertificatByExercice()
        {
            DataTable resDataTable = new DataTable();
            DBConnection dbConnection = new DBConnection();
            string query = "SELECT COUNT(id) AS nb_cert " +
                           "FROM crs_certificat " +
                           "WHERE id_exercice = " + IdExercice + " ";
            NpgsqlConnection conn = dbConnection.Conn;
            
            NpgsqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = query;
            Int32 nbCertByExercice = 0;
            try
            {
                conn.Open();
                NpgsqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    nbCertByExercice = reader.GetInt32(0);
                }
                MessageBox.Show("" + nbCertByExercice);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.StackTrace);
                MessageBox.Show(ex.Message);
            }
            conn.Close();
            return nbCertByExercice;
        }
我总是会遇到这样的错误:“指定的强制转换无效”
但当我使用这个:

while (reader.Read())
                {
                    nbCertByExercice = Int32.Parse(reader["nb_cert"].ToString());
                }
它很好用
我在dateTime类型上也有同样的问题

如何直接获取字段类型?

为什么不使用ExecuteScalar进行查询

替换

 NpgsqlDataReader reader = cmd.ExecuteReader();

 while (reader.Read())
 {
     nbCertByExercice = reader.GetInt32(0);
 }

当您有行要控制时,您应该使用reader,以获取查询第一列的第一个值,用于诸如Insert、delete之类的操作

更新

查看,您可以看到
COUNT
的返回类型是
int
,而不是
INt32
。当你表演的时候

reader.GetInt32(0);
您将获得InvalidCastException,因为它不执行类型()的显式转换

为无效强制转换或显式转换引发的异常 转换

否则,当您执行
Int32.Parse
时,它总是尝试转换,如果转换失败,则会出现异常


在您的情况下,该值始终有资格转换为int32,但由于类型不同,GetInt32无法识别;另一方面,
Int32.Parse
尝试并成功。

读卡器[“nb_cert”]的值是多少。ToString()?您确定吗?因此
计数(id)为nb_cert
返回
“int”
?如果您在数据库上执行该查询呢?COUNT return int right?它会给我数据库中的证书数!!如果你把这一行放在:
string s=reader[“nb_cert”].ToString()中,什么是
s
?有什么区别吗?抱歉还是一样的问题!!thnx用于解释:)
reader.GetInt32(0);