简体   繁体   English

如何从SQL Server获取ado.net中的数字数据类型的值

[英]How to get value of numeric datatype in ado.net from SQL Server

I am writing application with ADO.NET and want to select a value which is of type numeric(9, 2) . 我正在使用ADO.NET编写应用程序,并希望选择一个类型为numeric(9, 2) But I don't know how to use this value, I mean I'm not able to cast this value. 但是我不知道如何使用此值,我的意思是我无法转换此值。

So to which datatype it should be casted to. 因此应将其强制转换为哪种数据类型。

Decimal decimalLastModifiedWaitingForApprovalcurrency = 0;
decimalLastModifiedWaitingForApprovalcurrency = (Decimal)(cmd.ExecuteScalar());

I have tried this also 我也尝试过

Convert.ToDecimal(cmd.ExecuteScalar());

I have considerred that this could be casted to Decimal , But its giving error. 我考虑过可以将其强制转换为Decimal ,但是它给定错误。

So whats numeric equivalent of SQL server numeric datatype in C#? 那么C#中SQL Server数字数据类型的数字等效项是什么?

Since you are using ExecuteScalar() , it is returning the value as object . 由于您使用的是ExecuteScalar() ,因此它将值作为object返回。 If you aren't quite sure what CLI type it has chosen, just look at: 如果不确定所选择的CLI类型,请查看:

object value = cmd.ExecuteScalar();
Debug.WriteLine(value.GetType().Name);

Decimal only. Decimal You are using correct data type. 您正在使用正确的数据类型。

Check the complete list SQL Server Data Type Mappings 检查完整列表SQL Server Data Type Mappings

try Convert.ToDouble(cmd.ExecuteScalar()); 尝试Convert.ToDouble(cmd.ExecuteScalar()); and let me know if this does not work. 让我知道这是否行不通。

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

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