gpt4 book ai didi

c# - 指定的转换无效。是因为我试图转换一个物体吗?

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:59 25 4
gpt4 key购买 nike

我使用以下 C# 代码来尝试查询 oracle 数据库。我只是想获取数字结果,以便将其输出到控制台并最终将其保存在一个变量中。

虽然我不断收到以下错误:

System.InvalidCastException: Specified cast is not valid at Oracle.DataAccess.Client.OracleDataReader.GetInt32(Int32 i)

我在 Windows 专业机器上使用 VS 2012。如果我需要添加其他信息,请告诉我。非常感谢任何帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Oracle.DataAccess.Client;
using Oracle.DataAccess.Types;
using System.Configuration;
using System.Data.SqlClient;

namespace OB_837_File_Reconciliation_Rpt_Automation
{
class Program
{
static void Main(string[] args)
{
OracleConnection dbConnection;
string connectionString = "Data Source=(DESCRIPTION=(ADDRESS= (PROTOCOL=TCP)(HOST=*******)(PORT=******))(CONNECT_DATA=(SERVICE_NAME=******)));User ID=*****;Password=*********";
try
{
dbConnection = new OracleConnection(connectionString);
string query = "select count (*) AS AMG_Prof from wellmed_owner.claim c inner join WELLMED_OWNER.payment_detail pd on c.claim_id = pd.claim_id where c.claim_status in ('6','8','C') and c.insured_group_id in ('ASA','AEP') and c.form_type = '1' and trunc(pd.paid_date) = trunc(sysdate -4)";
dbConnection.Open();
OracleCommand comm = new OracleCommand(query, dbConnection);
OracleDataReader rdr = comm.ExecuteReader();
rdr.Read();
int count = (int)comm.ExecuteScalar();
Console.WriteLine(count);
//int num = rdr.GetInt32(0);
//Console.WriteLine(count);
Console.WriteLine("Connecting Okay");

}
catch (Exception e)
{
Console.WriteLine(e);
}

}
}
}

最佳答案

使用ExecuteScalar

dbConnection.Open();
OracleCommand comm = new OracleCommand(query, dbConnection);
decimal count = (decimal)comm.ExecuteScalar();
Console.WriteLine(count);
Console.WriteLine("Connecting Okay");

关于c# - 指定的转换无效。是因为我试图转换一个物体吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30834540/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com