if (kmatch == 1)
{
    con.Open();

    int a;
    a = Convert.ToInt16(txtBalance.Text);
    a = int.Parse(txtBalance.Text);

    OleDbCommand com = new OleDbCommand();
    com.Connection = con;
    String query = "update PlayerAccount set Balance='" + a + "'where Player_User=" + txtUser.Text + "";
    com.CommandText = query;
    com.ExecuteNonQuery();
    MessageBox.Show("PointCard Credited to your Account");
    con.Close();
}


起初,我认为它需要转换为int
但是现在我没主意了。

数据库
表:PlayerAccount

“余额”是一个整数

刚开始学习这个。任何帮助,将不胜感激。

最佳答案

您应该将String query = "update PlayerAccount set Balance='" + a + "'where Player_User=" + txtUser.Text + "";更改为String query = "update PlayerAccount set Balance= " + a + " where Player_User='" + txtUser.Text + "'";

您需要在SQL中的文本值两边加上单引号,而不必在数字值两边加上单引号(尽管您可以根据需要)。

但是,您似乎正在对SQL注入攻击敞开大门。

研究使用参数化命令而不是动态编写SQL。

SQL参数here的文档。

10-08 09:30
查看更多