【问题标题】:.NET Windows Form app mysql database query.NET Windows 窗体应用程序 mysql 数据库查询
【发布时间】:2022-01-04 02:36:28
【问题描述】:

我正在学习 C#,作为 .Net 练习的一部分,我需要给用户一个带有 from 、 to 和 amount 字段的表单,以及当用户点击转移按钮时。应用程序应从 from account_id 中扣除金额并将钱添加到 To account_id

这是我的按钮功能代码

string mysql_conn_string = "server=localhost;user=root;database=vsp;port=3306;password=password";
                MySqlConnection con = new MySqlConnection(mysql_conn_string);
                con.Open();
                string s1 = "update bank set amount = amount - " + textBox3.Text + "where account_id = " + textBox1.Text;
                string s2 = "update bank set amount = amount + " + textBox3.Text + "where account_id = " + textBox2.Text;

                MySqlTransaction tx = con.BeginTransaction();
                MySqlCommand cmd = new MySqlCommand(s1, con, tx);
                int a = cmd.ExecuteNonQuery();
                MySqlCommand cmd2 = new MySqlCommand(s2, con, tx);
                int b = cmd2.ExecuteNonQuery();

                if (a == 0 || b == 0)
                {
                    tx.Rollback();
                    MessageBox.Show("Rolling bacck");
                }
                else
                {
                    tx.Commit();
                    MessageBox.Show("Transaction Succesful");
                }
                con.Close();

我收到以下错误: 您的 SQL 语法有错误;检查与您的 MySqlserver 版本相对应的手册,以在第 1 行的“account_id = 2”附近使用正确的语法

但是同样的 sql 语句在 mysql workbench 中运行正常,并且没有显示任何错误。

我的表有 account_id 和 amount 列。

两者都是INT

我尝试使用 Int32.Parse 将文本框类型更改为 INT 和 Int64.Parse

【问题讨论】:

  • 在s1 和s2 创建后检查它们的值。这就是为什么你永远不应该连接值和字符串来创建 SQL 的 许多 原因之一。使用 DBParameters 总是
  • 谢谢你的问题是where语句之前没有空格。而且我还没有达到 C# 中的 DBParameters 阶段,但我将来肯定会使用它们,谢谢你的帮助

标签: c# mysql .net


【解决方案1】:

您的代码中存在许多严重问题

  1. 您的主要问题:由于缺少空格而导致语法错误。
  2. 如果您正确参数化查询以避免 SQL 注入,您会更快发现这一点。
  3. 您需要使用using 处理所有Sql 对象(然后您不需要显式调用Close)。
  4. 您可以一次执行这两个语句。
  5. 如果不起作用,则无需有条件地回滚。而是检查行是否存在。
  6. 您不应该对连接字符串进行硬编码,而是将其放入设置文件中。
  7. 不要在连接打开时用消息框阻塞线程。
using (var con = new MySqlConnection(Properties.Settings.ConnectionString))
{
    const string query = @"
start transaction;

if (select count(*)
    from bank
    where account_id in (@s1, @s2)
   ) = 2 then
    update bank
    set amount = amount + (case when account_id = @s1 then -@amt else @amt end)
    where account_id in (@s1, @s2);
end if;

commit;
";
    using (var cmd = new MySqlCommand(query, con))
    {
        cmd.Parameters.AddWithValue("@s1", textBox1.Text);
        cmd.Parameters.AddWithValue("@s2", textBox2.Text);
        cmd.Parameters.AddWithValue("@amt", textBox3.Text);
        con.Open();
        rows = cmd.ExecuteNonQuery();
    }
}

if (rows == 0)
{
    MessageBox.Show("Not executed");
}
else
{
    MessageBox.Show("Transaction Succesful");
}

【讨论】:

    【解决方案2】:

    据我所见,错误不在 WHERE 条件上,而是在数据上,因为您需要添加一个空格。你有这个:

    string s1 = "update bank set amount = amount - " + textBox3.Text + "where account_id = " + textBox1.Text;
    string s2 = "update bank set amount = amount + " + textBox3.Text + "where account_id = " + textBox2.Text;
    

    然后,当构建查询时,您将拥有:

    s1 = "update bank set amount = amount - 100where account_id = 1";
    

    如您所见,WHERE 和 textBox3 上的文本是在一起的,尝试像这样添加一个空格:

    string s1 = "update bank set amount = amount - " + textBox3.Text + " where account_id = " + textBox1.Text;
    

    【讨论】:

    • 感谢 Luis 的回答,由于某种原因您的回答显示迟了,我将删除我的回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多