0

i want insert some data in my database (heidisql). but it won't get inserted. any idea why?

in my table loginname,email,algo are varchar. hash salt are char. and stretch has the datatype int

MySqlCommand cmd2;

cmd2 = dbConStr.CreateCommand();
cmd2.CommandText = "INSERT INTO `user` (Loginname, Email, Algo, Stretch, `Hash`, Salt) VALUES ('@loginname', '@email', '@algo', '@stretch','@hash', '@salt');";

cmd2.Parameters.Add("@loginname", MySqlDbType.VarChar).Value = loginname;
cmd2.Parameters.Add("@email", MySqlDbType.VarChar).Value = email;
cmd2.Parameters.Add("@algo", MySqlDbType.VarChar).Value = algo;
cmd2.Parameters.Add("@stretch", MySqlDbType.VarChar).Value = Convert.ToInt32(stretch);
cmd2.Parameters.Add("@hash", MySqlDbType.VarChar).Value = hash;
cmd2.Parameters.Add("@salt", MySqlDbType.VarChar).Value = salt;

MySqlDataReader r2 = cmd.ExecuteReader();

while (r2.Read())
{ }

r2.Close();
catcat
  • 21
  • 6
  • Are you getting a specific error? – Salah Akbari Jan 08 '18 at 13:51
  • `ExecuteReader()`? Shouldn't that be `ExecuteNonQuery()`? What are you expecting to *read*, especially with an empty loop? Also, I don't think you need (or even want) quotes around the value parameters. And is that even the syntax for parameters in MySQL? It's been a while, but I thought it was just a `?` instead. – David Jan 08 '18 at 13:51
  • You can use `ExecuteNonQuery`....also `heidisql` is not a database, but, a client GUI...can you be more specific? – Hackerman Jan 08 '18 at 13:52
  • Omit the quotes around each parameter and use executeNonQuery instead of reader – apomene Jan 08 '18 at 13:56
  • @David depends on the database used, in SQL Server is '@' – Cleptus Jan 08 '18 at 13:57
  • @bradbury9: Agreed. In this case the use of `MySqlCommand` implies that it's not SQL Server though. – David Jan 08 '18 at 13:58
  • 1
    @David indeed, didnt notice, but looks like '@' is also valid. Almost duplicate of https://stackoverflow.com/questions/13580993/mysqlcommand-command-parameters-add-is-obsolete – Cleptus Jan 08 '18 at 14:02

1 Answers1

0

Beside the ExecuteReader, which should be ExecuteNonQuery(), you are declaring wrong the insert parameters, should not be single quoted. Try:

INSERT INTO `user` (Loginname, Email, Algo, Stretch, `Hash`, Salt) VALUES (@loginname, @email, @algo, @stretch, @hash, @salt)

BTW, there are some databases that have different parameter delimiter, not sure if in heidisql is the '@'

Cleptus
  • 3,446
  • 4
  • 28
  • 34