十进制错误

时间:2012-07-16 09:36:09

标签: c# asp.net

我的网络服务代码

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
    SqlConnection con = new SqlConnection(
        "Server=NOVA-PC;database=totev006;User ID=***;Password =***;");

    public Service () { }

    [WebMethod(Description = "Update return in Bet")]
    public string setReturn(int EventID)
    {
        string sql = "";
        decimal dcm = (decimal)100.5 + (decimal)45.055;
        int BetTypeWinID = 1;
        int BettorID = 1;
        int ins = 0;

        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = con;

        sql = "update bet";
        sql += " set [return] = " + dcm;
        sql += " where event_id = " + EventID;
        sql += " and bet_type_id = " + BetTypeWinID;
        sql += " and bettor_id = " + BettorID;

        cmd.CommandText = sql;
        ins += cmd.ExecuteNonQuery();
        con.Close();
        if (ins > 0)
            return "Ok";
        else
            return "not Ok";
    }
}

我得到了异常,这就是callstack:

System.Data.SqlClient.SqlException: Incorrect syntax near '555'.
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async)
at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(DbAsyncResult result, String methodName, Boolean sendToPipe)
at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
at Service.setReturn(Int32 EventID) in e:\Learning\Webservice\demo\webservice\App_Code\Service.cs:line 49

如何解决这个问题?

1 个答案:

答案 0 :(得分:3)

您的系统可能正在使用将123.45格式化为123,45的区域设置。

在格式设置中使用parameterized queries(请参阅SqlParameter)或be specific

无论你做什么,DO NOT "just" change the locale setting都要“解决”这个问题。