我尝试使用 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/