好的,所以我在许多 MANY 其他主题中看到了这个问题,但从来没有得到完整的答案(至少到目前为止)。 所以我有一个基本代码,用一个新用户将所有信息插入MySQL数据库(localhost),我基本上已经完成了相同的代码,并且它有效。 它不是句子的长度,因为该列可以处理至少40个字符。
错误:
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'
我的代码:
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()}'");
// }
}
好的,这是Register表单/窗口的代码,用户将输入数据并将其插入数据库。 " Query.Check"是另一个类(Query),我在其中编写SQL方法,以重用代码并节省空间。 哦和#34; .Encriptar()"只是一种散列密码的方法。 这是:
public static long Execute(string query)
{
var con = Connection.Create();
var command = new MySqlCommand(query, con);
command.ExecuteNonQuery();
con.Close();
return command.LastInsertedId;
}
所以......我不知道为什么这不起作用,因为我使用之前的应用做了完全相同的事情并且工作得很好! 请有人帮助我。 顺便说一句,我是新来的,很抱歉,如果我没有正确地写这篇文章。
答案 0 :(得分:0)
你在sql中缺少一个
, Nombre_Usu='{NombreUsu.Text}' Clave='{txtContra.Password.Encriptar()}'
需要
, Nombre_Usu='{NombreUsu.Text}', Clave='{txtContra.Password.Encriptar()}'
除此之外,使用paramatized查询(这里有很多问题)
答案 1 :(得分:0)
INSERT的SQL语法不正确。它应该是
INSERT INTO usuario (Nombre, Apellido, Nombre_Usu, Clave) VALUES (@Nombre, @Apellido, @Nombre_Usu, @Clave)
同样,Jon在评论中表示,您需要参数化您的查询。