简体   繁体   中英

Cannot convert a decimal value to money, C# -> SP SQL Server

I want to execute a stored procedure, and give it a parameter like this:

new SqlParameter("@ResidualValue", importExportOptions.ResidualValue)

ResidualValue is of type decimal, and in the database, it has type Money. Now I've been looking for an answer of course, and found the following:

"Just initialize the Parameter with a new SqlMoney(decimal) constructor. SqlParameter parameter = new SqlParameter("@Money", SqlDbType.Money);"

But adding SqlDbType.Money does not give me the option to specify the value importExportOptions.ResidualValue anymore. What can I do?

I think you are looking for something this;

command.Parameters.Add(
    new SqlParameter("@ResidualValue", SqlDbType.Money).Value = importExportOptions.ResidualValue;

Though this syntax is now deprecated as this quote from MSDN shows:

AddWithValue replaces the SqlParameterCollection.Add method that takes a String and an Object. The overload of Add that takes a string and an object was deprecated because of possible ambiguity with the SqlParameterCollection.Add overload that takes a String and a SqlDbType enumeration value where passing an integer with the string could be interpreted as being either the parameter value or the corresponding SqlDbType value. Use AddWithValue whenever you want to add a parameter by specifying its name and value.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparametercollection.addwithvalue%28v=vs.110%29.aspx

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