简体   繁体   中英

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) . 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.

So whats numeric equivalent of SQL server numeric datatype in C#?

Since you are using ExecuteScalar() , it is returning the value as object . If you aren't quite sure what CLI type it has chosen, just look at:

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

Decimal only. You are using correct data type.

Check the complete list SQL Server Data Type Mappings

try Convert.ToDouble(cmd.ExecuteScalar()); and let me know if this does not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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