gpt4 book ai didi

c# - ExecuteNonQuery() 语法错误 c#

转载 作者:行者123 更新时间:2023-11-29 18:30:35 24 4
gpt4 key购买 nike

好的,所以我在许多许多其他线程中看到过这个问题,但从未得到完整的答案(至少到目前为止)。所以我有一个基本代码来将一个新用户及其所有信息插入到 MySQL 数据库(本地主机)中,并且我之前基本上已经完成了相同的代码,并且它有效。它不是句子的长度,因为该列可以处理至少 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()}'");
// }

}

好吧,这是注册表单/窗口的代码,用户将在其中输入数据并将其插入数据库中。 “Query.Check”是另一个类(Query),我在其中编写了 SQL 方法,以重用代码并节省空间。哦,“.Encriptar()”只是一种散列密码的方法。这是:

     public static long Execute(string query)
{
var con = Connection.Create();
var command = new MySqlCommand(query, con);
command.ExecuteNonQuery();
con.Close();
return command.LastInsertedId;
}

所以...我不知道为什么这不起作用,因为我对以前的应用程序做了完全相同的事情并且工作得很好!请有人帮助我。顺便说一句,我是新来的,如果我没有正确写这篇文章,很抱歉。

最佳答案

你的sql中缺少一个 ,

, Nombre_Usu='{NombreUsu.Text}' Clave='{txtContra.Password.Encriptar()}'

需要

, Nombre_Usu='{NombreUsu.Text}', Clave='{txtContra.Password.Encriptar()}'

除此之外,使用参数化查询(这里有很多关于此的问题)

关于c# - ExecuteNonQuery() 语法错误 c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45723453/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com