F# 和 ExecuteScalar 转换
如何从 F# 使用 DbCommand.ExecuteScalar? 它返回一个 obj,我需要将其转换为 int。 我对 F# 非常陌生,我需要做的转换还不清楚。
let (stockid:uint32) = command.ExecuteScalar()
Compile Error:
Type constraint mismatch. The type obj is not compatible with type uint32
使用 :?> 向上转换 抛出运行时错误。
How can I use DbCommand.ExecuteScalar from F#? It returns an obj which I need to convert to an int. I'm very new to F# and the casting I need to do is not clear.
let (stockid:uint32) = command.ExecuteScalar()
Compile Error:
Type constraint mismatch. The type obj is not compatible with type uint32
Upcasting using :?> throws a runtime error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你只说
你会得到什么? 它确实是一个 int 吗? (int32 或 uint32 还是什么?)
:?> 运算符是正确的向下转型运算符,但您需要类型匹配。 转换为实际类型后,如果需要从一种整型转换为另一种整型,请使用目标类型相应的函数,例如
If you just say
what do you get? Is it indeed an int? (int32 or uint32 or what?)
The :?> operator is the correct downcast operator, but you need the types to match. After casting to the actual type, if you need to convert from one integral type to another, then use the corresponding function for the destination type, e.g.
如果 Downcast (:?>) 在运行时抛出异常,则您不会从执行标量获得 unit32 作为返回值。 你应该能够转换...
但是如果你得到的东西无法转换为 uint32,那也会失败。有关转换和转换的更多信息请参见此处。
If the Downcast (:?>) throws at runtime, you're not getting a unit32 as a return value from the Execute Scalar. You should be able to convert ...
But if you're getting something that can't be converted to an uint32, that will fail too.More on casting and converting here.
尝试将其转换为 int32 而不是 uint32。 ExecuteScalar 返回一个 int32 类型的对象。
Try casting it to an int32 instead of a uint32. ExecuteScalar returns an object of int32.