2012-07-16 81 views
1

我的web服務代碼錯誤十進制

[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"; 
    } 
} 

我得到的例外,這是調用堆棧:

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

它聞到小數點分隔符錯誤。嘗試在您的SqlCommand中使用SqlParameter。 – tschmit007 2012-07-16 09:39:42

+0

'Console.WriteLine(sql)'請。 – leppie 2012-07-16 09:40:25

+0

@user [數據庫中的返回]類型是十進制數? – 2012-07-16 09:43:01

回答