Returning a single row

前端 未结 10 1439
轻奢々
轻奢々 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:41

    the reader returns object which you should cast it to what you need, in this case a string.

    you can use any of this codes :

    return reader.GetString(0);

    return reader["col_1"].ToString();

    return Convert.ToString(reader["col_1"]);

    return reader["col_1"] as string;

    but dont forget to close the connection and reader before leaving the function.

    string ret = reader.GetString(0);
    reader.Close();
    connection.Close();
    return ret;
    

提交回复
热议问题