简体   繁体   English

在C#中将SQLite和Int64转换为Int32

[英]SQLite & Int64 to Int32 cast in C#

Good night, 晚安,

I have an SQLite query which I am executing in C# using the ExecuteScalar() command. 我有一个使用ExecuteScalar()命令在C#中执行的SQLite查询。 The result in an Object type variable which is in fact a value of type Int64. Object类型变量的结果,实际上是Int64类型的值。 As I need to cast it to an Int32, I tried first using Convert.ToInt32 and now I am using (Int32)(Int64)Object , although I get the same performance with both. 由于需要将其(Int32)(Int64)Object转换为Int32,因此我首先尝试使用Convert.ToInt32 ,现在我使用的是(Int32)(Int64)Object ,尽管两者的性能相同。 The question is that this kind of cast seems to take an additional 6ms to perform, and the method concerned is critical code which should be executed as fast as possible... Is there any way to perform this cast faster, by using, for example, bit operators or some other method? 问题是这种类型的转换似乎需要花费额外的6毫秒来执行,并且相关的方法是关键代码,应尽快执行...是否有任何方法可以更快地执行这种类型的转换,例如使用,位运算符或其他方法?

Just in case this is not possible, I really don't understand why the result of the query is an Int64... is there any way I can force it to be an Int32? 万一这是不可能的,我真的不明白为什么查询的结果是Int64 ...有什么办法可以强迫它成为Int32?

Thank you very much. 非常感谢你。

You could use a data reader instead of execute scalar and call the static getint32 function. 您可以使用数据读取器代替执行标量,然后调用静态getint32函数。 Not sure if it would be faster though. 不确定是否会更快。

Alternatively, you can cast your result in the sql query before it is returned. 或者,您可以在返回结果之前将结果强制转换为sql查询。 Again, this may actually decrease the overall performance. 同样,这实际上可能会降低整体性能。

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

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