[英]ExecuteNonQuery() Syntax Error c#
Ok, so I've seen this question in many MANY other threads, but never get a complete answer (at least so far). 好了,我已经看到了许多许多其他线程这个问题,但从来没有得到一个完整的答案(至少到目前为止)。 So I have a basic code to insert a new user with all of it's info into a MySQL database (localhost), and I have basically done the same code a previous time, and it worked.
因此,我有一个基本代码,可以将包含所有信息的新用户插入到MySQL数据库(本地主机)中,并且上一次我基本上完成了相同的代码,并且可以正常工作。 It isn't the length of the sentence, since the column can handle at least 40 chars.
这不是句子的长度,因为该列可以处理至少40个字符。
The error: 错误:
MySql.Data.MySqlClient.MySqlException: 'You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Clave='9cdfb439c7876e703e307864c9167a15'' at line 1'
My code: 我的代码:
private void Button_Click(object sender, RoutedEventArgs e)
{
var nomCom = Nombre.Text + Apellido.Text;
/*var exists = Query.Check($"SELECT EXISTS(SELECT 1 FROM usuario WHERE Nombre_Usu='{NombreUsu.Text}'");
if (!exists)
{
MessageBox.Show("Nombre de usuario ya existe","VERIFIQUE",MessageBoxButton.OK,MessageBoxImage.Exclamation);
}
else
{*/
if (string.IsNullOrWhiteSpace(nomCom) || string.IsNullOrWhiteSpace(NombreUsu.Text) || txtContra.Password.ToString() != txtConfirmar.Password.ToString())
{
MessageBox.Show("Verifique los campos porfavor :)", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
return;
}
Query.Execute($"INSERT usuario SET Nombre='{Nombre.Text}', Apellido='{Apellido.Text}', Nombre_Usu='{NombreUsu.Text}' Clave='{txtContra.Password.Encriptar()}'");
// }
}
Alright, that is the code for the Register form/window, where the user will input the data and insert that into the DB. 好的,这是“注册”表单/窗口的代码,用户将在其中输入数据并将其插入到DB中。 The "Query.Check" is another class (Query) in which I wrote the SQL methods, to reuse code and save space.
“ Query.Check”是另一个类(查询),我在其中编写了SQL方法,以重用代码并节省空间。 Oh and the ".Encriptar()" is just a method to hash the password.
哦,“。Encriptar()”只是散列密码的一种方法。 Here it is:
这里是:
public static long Execute(string query)
{
var con = Connection.Create();
var command = new MySqlCommand(query, con);
command.ExecuteNonQuery();
con.Close();
return command.LastInsertedId;
}
So... I don't know why this isn't working, since I did exactly the same thing with a previous app and worked just fine! 所以...我不知道为什么这行不通,因为我在以前的应用程序中做了完全一样的事情,并且工作正常! Please, someone help me.
拜托,有人帮我。 Btw, I'm new here, so sorry if I didn't write the post correctly.
顺便说一句,我是新来的,如果我没有正确写这篇文章,对不起。
you are missing a , in your sql 您在SQL中缺少,
, Nombre_Usu='{NombreUsu.Text}' Clave='{txtContra.Password.Encriptar()}'
needs to be 需要是
, Nombre_Usu='{NombreUsu.Text}', Clave='{txtContra.Password.Encriptar()}'
Beyond that, use paramatized queries ( lots of questions on here about that ) 除此之外,请使用参数化查询(关于此的许多问题)
Your SQL syntax for the INSERT is incorrect. 您的INSERT的SQL语法不正确。 It should be
它应该是
INSERT INTO usuario (Nombre, Apellido, Nombre_Usu, Clave) VALUES (@Nombre, @Apellido, @Nombre_Usu, @Clave)
Also as Jon stated in the comments, you will want to parameterize your query. 同样,如乔恩在评论中所述,您将需要参数化您的查询。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.