c# - 使用 C# 从 Oracle DB 返回 Int 值

标签 c# database oracle

我尝试使用 C# 从 Oracle 数据库返回 INTEGER 值,但它给了我以下异常

An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll

Additional information: Specified cast is not valid.

谁能帮我解决这个问题吗?

这是我的代码:

OleDbConnection con = new OleDbConnection(
  "Provider=MSDAORA;Data Source=xe;User ID=hr; password=123");
   con.Open();

String cmd1 = 
  @"select retail_price,purchase_price from 
    productdetails where item_name='" + textBox1.Text + "'";

OleDbCommand a1 = new OleDbCommand(cmd1, con);
OleDbDataReader myReader = a1.ExecuteReader();

if (myReader.Read())
{
    int m = myReader.GetInt32(0);
    MessageBox.Show("" +m);
}

最佳答案

首先根据查询

    select retail_price,
           purchase_price
      from productdetails 
     where item_name = :item_name

retail_price 以及 purchase_price 的实际类型可以是某种浮点值类型(double十进制;例如,8.95$ == 8.95):

   decimal retail_price = Convert.ToDecimal(myReader[0]);
   decimal purchase_price = Convert.ToDecimal(myReader[1]);

如果您坚持使用整数,请将其四舍五入 (8.95$ == 8.95 -> 9);假设价格非负:

   int retail_price = (int) (Convert.ToDouble(myReader[0]) + 0.5);
   int purchase_price = (int) (Convert.ToDouble(myReader[1]) + 0.5);

关于c# - 使用 C# 从 Oracle DB 返回 Int 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46972131/

相关文章:

c# - Linq Distinct() 按名称用名称和值填充下拉列表

c# - 如何获取类的枚举属性列表?

mysql - 在数据库表中有额外的数据被认为是一种不好的做法吗?

database - 哪个性能更好 - 将一个表分成两个或增加列数 - Oracle

java - 如何在Java中解析这个 "31/10/13"到Oracle日期

java - 使用 JSP 将 Excel 数据上传到 Oracle 数据库

oracle - Oracle 中的性能独立过程与打包过程

c# - 将 DLL 加载到单独的 AppDomain 中

c# - 旋转 BitmapImage

database - 在集中设置中需要 redis 服务器