Returning a single row

前端 未结 10 2301
野的像风
野的像风 2021-02-13 06:58

I\'m trying to return a single row from a database:

using (connection = new SqlConnection(ConfigurationManager.AppSettings[\"connection\"]))
{
    using (command         


        
10条回答
  •  隐瞒了意图╮
    2021-02-13 07:37

    Instead of:

     using (reader = command.ExecuteReader())
     {
          reader.Read();
          return reader["col_1"];
     }
    

    You need to cast the reader["col_1"] to string, either reader["col_1"].ToString() or reader.GetString(0) like:

    return reader.GetString(0);
    

提交回复
热议问题