Returning a single row

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

    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);
        }
    }
    [...]
    

提交回复
热议问题