gpt4 book ai didi

c# - 插入期间未处理 MySqlException

转载 作者:行者123 更新时间:2023-11-29 13:00:43 29 4
gpt4 key购买 nike

我正在使用 MySql 创建 Visual Studios 注册表单,但不断收到错误:

Fatal error encountered during command execution

知道是什么原因造成的吗?

MySqlConnection connection;
string cs = @"server=127.0.0.1;userid=root;password=welcome;database=userinfo";
connection = new MySqlConnection(cs);
connection.Open();

MySqlCommand command = new MySqlCommand();
string SQL = "INSERT INTO `users` (`userid`, `email`, `passone`, `passtwo`,'lastname','firstname') VALUES (@userid_txt, @email_txt, @passone_txt, @passtwo_txt, @lastname_txt, @firstname_txt)";
command.CommandText = SQL;
command.Parameters.AddWithValue("userid", userid_txt);
command.Parameters.AddWithValue("email", email_txt);
command.Parameters.AddWithValue("passone", passone_txt);
command.Parameters.AddWithValue("passtwo", passtwo_txt);
command.Parameters.AddWithValue("lastname", lastname_txt);
command.Parameters.AddWithValue("firstname", firstname_txt);
command.Connection = connection;

command.ExecuteNonQuery();

connection.Close();

内部异常状态:

Parameter '@userid_txt' must be defined

最佳答案

在语句中使用参数时,例如:

 .. VALUES (@param1, @param2, ...

应该有相应的参数。 AddXXX() 与参数文本匹配的行,例如:

command.Parameters.AddWithValue("param1", somevalue);
command.Parameters.AddWithValue("param2", othervalue);

就您而言,您的参数标签不匹配:

 .. VALUES (@userid_txt, @email_txt, ...

其中“userid_txt”需要在参数集合中指定:

command.Parameters.AddWithValue("userid_txt", userid_txt);

您拥有“userid”而不是“userid_txt”。其他参数也是如此。

关于c# - 插入期间未处理 MySqlException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23379021/

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