0

如何使用 F# 中的 DbCommand.ExecuteScalar?它返回一个我需要转换为 int 的 obj。我对 F# 很陌生,我需要做的演员表还不清楚。

let (stockid:uint32) = command.ExecuteScalar()

Compile Error: 
Type constraint mismatch. The type   obj is not compatible with type  uint32 

使用 :?> 向上转换会引发运行时错误。

4

3 回答 3

4

如果你只是说

let o : obj = ...
printfn "%s" (o.GetType().ToString())

你得到了什么?它确实是一个int吗?(int32 或 uint32 还是什么?)

:?> 运算符是正确的向下转换运算符,但您需要匹配类型。转换为实际类型后,如果您需要从一种整数类型转换为另一种整数类型,则使用目标类型的相应函数,例如

let x : int = int myUnsignedInt  
// first 'int' is type, second is function to convert-to-int
于 2009-06-02T05:27:13.517 回答
0

如果 Downcast (:?>) 在运行时抛出,您不会从 Execute Scalar 获得 unit32 作为返回值。你应该可以转换...

let stockid : uint32 = unit32 command.ExecuteStalar()

但是如果你得到的东西不能转换为 uint32,那也会失败。更多关于铸造和转换在这里

于 2009-06-02T05:28:46.667 回答
0

尝试将其转换为 int32 而不是 uint32。ExecuteScalar 返回一个 int32 的对象。

于 2009-06-02T05:29:36.480 回答