方法'ToString'没有重载需要1个参数

时间:2016-12-22 17:41:54

标签: c# asp.net sql-server asp.net-web-api

我有一个Web API运行存储过程并从表中返回记录。记录包含名为CounterSeq的int字段

[HttpGet]
public IHttpActionResult Get(string Account)
{
  // other code for connecting to the SQL seerver and calling the stored procedure
 reader = command.ExecuteReader(); 
 List<QueryResult>qresults = new List<QueryResult>();
  while (reader.Read())
 {  
   QueryResult qr = new QueryResult();
   qr.AccountID = reader["AccountID"].ToString();
   qr.CounterSeq = reader["CounterSeq"].ToString("000");
   qresults.Add(qr);
 }
 DbConnection.Close();
 return Ok(qresults); 

让CounterSeq的响应为“001”,“010”,“100”。它会导致No overload for method 'ToString' takes 1 arguments

中的reader["CounterSeq"].ToString("000");错误

1 个答案:

答案 0 :(得分:5)

reader["CounterSeq"]返回object首先您需要将其投放到int,然后再调用ToString("000"),如下所示:

((int)reader["CounterSeq"]).ToString("000")