简体   繁体   English

将ODBC十进制数转换为C#int的最干净方法是什么?

[英]What's the cleanest way to get an ODBC decimal to C# int?

I working on some code that queries an DB2 database via an ODBC connection. 我正在研究一些通过ODBC连接查询DB2数据库的代码。 The value represents an employee number, but it is stored in the DB as a decimal. 该值表示员工编号,但以十进制形式存储在数据库中。 There are several columns like this, so I am looking for the cleanest and safest way to convert the values to ints. 像这样的几列,所以我正在寻找将值转换为int的最干净最安全的方法。

I can try dr.GetInt32(colIndex) , but I try to avoid using column ordinals to minimized confusion. 我可以尝试使用dr.GetInt32(colIndex) ,但是我尽量避免使用列序数来最大程度地减少混乱。 I didn't see a method on dr["MyCol"].GetOrdinal() . 我在dr["MyCol"].GetOrdinal()上没有看到任何方法。

I can do it in two steps, but I don't want this method to get really long with conversion. 我可以分两步来完成,但是我不希望这种方法在转换时花很长时间。 Any suggestions? 有什么建议么?

(int)dr.GetDecimal(dr.GetOrdinal("MyCol"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM