Returning a single row

前端 未结 10 1366
轻奢々
轻奢々 2021-02-13 06:55

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:23

    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);
    

提交回复
热议问题