.Net MySql error “The given key was not present in the dictionary”

后端 未结 7 1369
深忆病人
深忆病人 2021-01-19 01:44

Trying to get simple count from table results in exception bellow. Tried different select statemens which also makes exception: \"SELECT * FROM goods\", but \"<

7条回答
  •  野的像风
    2021-01-19 02:44

    In place of this statement:

    using (MySqlCommand cmd = new MySqlCommand("SELECT count(*) FROM goods", conn))
    

    Use:

    using (var cmd = new MySqlCommand("SELECT COUNT(*) FROM goods", conn))
    

    and then convert it to int value by using ExecuteScalar(). Something like this:

    int count = Convert.ToInt32(cmd.ExecuteScalar());
    

提交回复
热议问题