Returning a single row

前端 未结 10 2311
野的像风
野的像风 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:24

    You can use an if statement if your query only returns one value

    [...]
    string x = string.Empty;
    if(reader.Read()) {
        // make sure the value is not DBNull
        if(DBNull.Value != reader["col_1"]) {
           x = reader.GetString(0);
        }
    }
    [...]
    

提交回复
热议问题