我有一个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");
错误
答案 0 :(得分:5)
reader["CounterSeq"]
返回object
首先您需要将其投放到int
,然后再调用ToString("000")
,如下所示:
((int)reader["CounterSeq"]).ToString("000")