简体   繁体   中英

C#, MySql Select statement

I can't figure out what's wrong, I've checked other questions about it. I get the following message in my textbox: "mysql.data.mysqlclient.mysqlcommand"

if (Session["Login"] != null)
    {           
        string email = (string)Session["Login"];
        MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringloginDb"].ConnectionString);
        conn.Open();

        MySqlCommand getNome = conn.CreateCommand();
        getNome.CommandType = CommandType.Text;
        getNome.CommandText = "SELECT nome_utente FROM utenti WHERE email = @email";
        getNome.Parameters.AddWithValue("@email", email);
        getNome.ExecuteNonQuery();

        txtNome.Text = getNome.ToString();

        conn.Close();

etc.

getNome is a MySqlCommand which doesn't override ToString . Therefore you get the full-type-name when you call getNome.ToString . You want this:

// getNome.ExecuteNonQuery();                    <--- not this
txtNome.Text = (String)getNome.ExecuteScalar();  <--- but this

( presuming that email is unique )

尝试

 txtNome.Text = (string)getNome.ExecuteScalar();

because you are using the .ToString() method of the MySqlCommand type, which in this case returns the name of the class itself (inherited from objet type).

I'd suggest to use the ExecuteScalar() method instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM