gpt4 book ai didi

c# - 我不断收到输入字符串的格式不正确的信息

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

我认为我写的所有内容都是正确的...但我不断收到此错误,说输入字符串的格式不正确

所以首先我从用户那里获取值,然后转换为 int 然后计算然后将其返回到字符串

private void btnenroll_Click(object sender, RoutedEventArgs e)
{
string things;
string morethings;
try
{
id = Convert.ToInt32(txtempty.Text) + 1;
con.Open();

things = "INSERT INTO vikoba.members(id_no , names_) VALUES (@id) , (@name)";
cmd = new MySqlCommand(things, con);

cmd.Parameters.Add(new MySqlParameter( "@id", MySqlDbType.Int32,4));
cmd.Parameters["@id"].Value = id.ToString();
cmd.Parameters.Add(new MySqlParameter("@name", MySqlDbType.VarChar,20));
cmd.Parameters["@name"].Value = txtname.Text;

cmd.ExecuteNonQuery();

morethings = "INSERT INTO vikoba.mpoko(id_no , jumpla_mpoko , mkopo , riba , bima) VALUES (@id) , (@jumla) , (@mkop) , (@riba) ,(@bima) ";
cmd = new MySqlCommand(morethings, con);
cmd.Parameters.Add(new MySqlParameter("@id", MySqlDbType.Int32));
cmd.Parameters["@id"].Value = id.ToString();
cmd.Parameters.Add(new MySqlParameter ("@jumla", MySqlDbType.Int32));
cmd.Parameters["@jumla"].Value = txtjumla.Text;
cmd.Parameters.Add(new MySqlParameter("@mkop",MySqlDbType.Int32));
cmd.Parameters["@mkop"].Value = txtloan.Text;
cmd.Parameters.Add(new MySqlParameter ("@riba", MySqlDbType.Int32));
cmd.Parameters["@riba"].Value = txtriba.Text;
cmd.Parameters.Add(new MySqlParameter("@bima", MySqlDbType.Int32));
cmd.Parameters["@bima"].Value = txtbima.Text;
cmd.ExecuteNonQuery();

con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

最佳答案

您的 SQL 查询语法错误

things = "INSERT INTO vikoba.members(id_no , names_) VALUES (@id, @name)";
cmd = new MySqlCommand(things, con);
cmd.Parameters.Add(new MySqlParameter( "@id", MySqlDbType.Int32,4));
cmd.Parameters["@id"].Value = id;
cmd.Parameters.Add(new MySqlParameter("@name", MySqlDbType.VarChar,20));
cmd.Parameters["@name"].Value = txtname.Text;
cmd.ExecuteNonQuery();

删除了VALUES(@ID), (@name),并且在将 id 变量作为参数值传递时不要尝试将其转换为字符串。第二个查询中发生同样的事情

 morethings = "INSERT INTO vikoba.mpoko(id_no , jumpla_mpoko , mkopo , riba , bima) " + 
"VALUES (@id, @jumla, @mkop, @riba, @bima)";

当然,文本框中的值应该是整数,否则该行的所有内容都会失败。确保使用 TryParse 更改该代码

 int result
if(!Int32.TryParse(txtempty.Text, out result)
{
MessageBox.Show("Not a valid integer number");
return;
}
id = result + 1;
....

再次查看查询的第二部分,用作参数输入的另一个文本框还存在其他潜在问题。每个参数都应该是 Int32,但您将字符串传递给参数值,并且您应该绝对确保它们是可转换为 Int32 的数字。查看所有内容,您将更改为此代码

private void btnenroll_Click(object sender, RoutedEventArgs e)
{
string things;
string morethings;
try
{
int id;
if(!Int32.TryParse(txtempty.Text, out id)
{
MessageBox.Show("Not a valid ID");
return;
}
int jumla;
if(!Int32.TryParse(txtjumla.Text, out jumla)
{
MessageBox.Show("Not a valid JUMLA");
return;
}
int mkop;
if(!Int32.TryParse(txtloan.Text, out mkop)
{
MessageBox.Show("Not a valid MKOP");
return;
}
....
.... and so on for the other integer required by the second query
....

id += 1;
con.Open();

things = "INSERT INTO vikoba.members(id_no , names_) VALUES (@id ,@name)";
cmd = new MySqlCommand(things, con);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@name", txtname.Text);
cmd.ExecuteNonQuery();

morethings = "INSERT INTO vikoba.mpoko(id_no , jumpla_mpoko , mkopo , riba , bima) " +
"VALUES (@id, @jumla, @mkop, @riba, @bima) ";
cmd = new MySqlCommand(morethings, con);
cmd.Parameters.AddWithValue("@id", id);
cmd.Parameters.AddWithValue("@jumla", jumla);
cmd.Parameters.AddWithValue("@mkop", mkop);
.... other parameters ...
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
}

关于c# - 我不断收到输入字符串的格式不正确的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20847533/

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