gpt4 book ai didi

c# - 使用 double 有问题,我被困在某个地方?

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

我想给一个整数加上 0.50,只是我很困惑。我不知道该怎么办。这是代码;

if (checkBox1.Checked)
{
keko.Open();

double mesai = Convert.ToDouble(textBox4.Text);
mesai = mesai + 0.50;
double maaş = Convert.ToDouble(textBox4.Text);
maaş += 1;
textBox4.Text = mesai.ToString() + maaş.ToString();

MySqlCommand cmd = new MySqlCommand(
"Update calisanlist Set Gun '" + textBox4.Text + "' where (Adı,Soyadı) '" + textBox1.Text + "','"+textBox2.Text+"'",
keko);

cmd.ExecuteNonQuery();

keko.Close();

按钮下方有一个复选框。如果我按下按钮而不选中该框,它会自行添加 1 好吧,这里没有问题。但是,当我选中该框并按下按钮时,我收到一条错误消息。

MySql.Data.MySqlClient.MySqlException:

'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''7,58' where (Adı,Soyadı) 'Egemen','Ören'' at line 1'.

我不知道如何解决,请帮助我

最佳答案

看来你很可能遇到了问题

   textBox4.Text = mesai.ToString() + maaş.ToString();

想象一下,mesai3.5maaş4.1 :您将拥有3.54.1textBox4.Text这不是有效 double值(value)。你可能是说

   double mesai = Convert.ToDouble(textBox4.Text) + 0.5;
//TODO: check isn't it a typo? Both mesai and maaş are from the same textBox4.Text
double maaş = Convert.ToDouble(textBox4.Text) + 1;

textBox4.Text = (mesai + maaş).ToString();

不要对查询进行硬编码,而是对其进行参数化

   ... 

// Keep sql readable
// and parameterized
string sql =
@"update calisanlist
set Gun = @prm_Gun
where Adı = @prm_Adi and
Soyadı = @prm_Soyadı";

// wrap IDisposable into "using"
using (MySqlCommand cmd = new MySqlCommand(sql, keko)) {
// I don't know underlying database types, that's why I've put AddWithValue
//TODO: change syntax to cmd.Parameters.Add(name, datatype).Value = ...
cmd.Parameters.AddWithValue("@prm_Gun", Convert.ToDouble(textBox4.Text));
cmd.Parameters.AddWithValue("@prm_Adi", textBox1.Text);
cmd.Parameters.AddWithValue("@prm_Soyadı", textBox2.Text);

cmd.ExecuteNonQuery();
}
...

关于c# - 使用 double 有问题,我被困在某个地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52264584/

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