gpt4 book ai didi

c# - 列名不能为空

转载 作者:行者123 更新时间:2023-11-30 22:31:52 25 4
gpt4 key购买 nike

它下面是我的代码..我在平台上尝试了几个答案,但它仍然给我同样的错误..有人请帮帮我!!!!提前致谢!

public bool saveRecord(string Firstname,string Lastname,string Username,string Password,string Emailadress)
{
using (MySqlConnection connection = new MySqlConnection("Server=localhost;Port=8889;Database=SSC;Uid=root;Password=root"))
{
try
{
string Sql_Query = "INSERT INTO administrator (Firstname,Lastname,Username,Password,EmailAdress)VALUES(@Firstname,@Lastname,@Username,@Password,@EmailAdress);";
connection.Open();
using (MySqlCommand command = new MySqlCommand(Sql_Query, connection))
{
command.Parameters.AddWithValue("@Firstname", Firstname);
command.Parameters.AddWithValue("@Lastname", Lastname);
command.Parameters.AddWithValue("@Username", Username);
command.Parameters.AddWithValue("@Password", Password);
command.Parameters.AddWithValue("@EmailAdress", Emailadress);

command.ExecuteNonQuery();
MessageBox.Show("Inserted");
}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}

测试类.....

Models model = new Models();
if (model.saveRecord(TbFisrtname.Text, TbLastname.Text,TbUsername.Text,TbPassword.Text,TbAddress.Text) == true)
{
MessageBox.Show("Inserted");
}

最佳答案

这表明 FirstName 参数变量在传递到方法时为 null。我建议这样验证你的论点:

    public bool saveRecord(string Firstname,string Lastname,string Username,string Password,string Emailadress)
{

if (string.IsNullOrWhiteSpace(FirstName))
throw new ArgumentException(FirstName, "FirstName cannot be null");
// Do the same for the other args...

using (MySqlConnection connection = new MySqlConnection("Server=localhost;Port=8889;Database=SSC;Uid=root;Password=root"))
{
try
{
string Sql_Query = "INSERT INTO administrator (Firstname,Lastname,Username,Password,EmailAdress)VALUES(@Firstname,@Lastname,@Username,@Password,@EmailAdress);";
connection.Open();

using (MySqlCommand command = new MySqlCommand(Sql_Query, connection))
{

command.Parameters.AddWithValue("@Firstname", Firstname);
command.Parameters.AddWithValue("@Lastname", Lastname);
command.Parameters.AddWithValue("@Username", Username);
command.Parameters.AddWithValue("@Password", Password);
command.Parameters.AddWithValue("@EmailAdress", Emailadress);

command.ExecuteNonQuery();
MessageBox.Show("Inserted");

}
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
}

关于c# - 列名不能为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33678341/

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