繁体   English   中英

如何在数据库中输入成绩?

[英]how to put grades in the database?

我不确定这是如何工作的,但无法将测验成绩放入学生桌,有人可以帮忙吗? 这是我的代码。

private void button9_Click(object sender, EventArgs e)
    {
        int x = 0;

        if (textBox20.Text == "yours" || textBox20.Text == "Yours" || textBox20.Text == "YOURS")
        {
            x++;
        }
        if (textBox21.Text == "mine" || textBox21.Text == "Mine" || textBox21.Text == "MINE")
        {
            x++;
        }
        if (textBox22.Text == "his" || textBox22.Text == "His" || textBox22.Text == "HIS")
        {
            x++;
        }
        if (textBox23.Text == "yours" || textBox23.Text == "Yours" || textBox23.Text == "YOURS")
        {
            x++;
        }         
        SqlConnection con = new SqlConnection(connStr);
        str = "UPDATE Student SET (Q1 ='" + x + "') WHERE (Username = '" + this.textBox3.Text + "' , Password = '" + this.textBox4.Text + "')";
        try
        {
            this.studentTableAdapter5.Update(this.cAIDataSet5.Student);
            MessageBox.Show("Your Score is: " + x);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

我不确定哪里出了问题,根本没有显示任何错误。

您不会在任何地方执行sql查询。

您需要按照以下步骤做一些事情:

using(var con = new SqlConnection(connStr))
{
   var command = new SqlCommand(str, connStr);
   command.ExecuteNonQuery(command);
}

正如评论员提到的那样,您的方法容易受到Sql注入的攻击。 尽管问题的背景使这一点变得无关紧要,但还是要意识到这一点。 您可以在此处阅读有关sql注入的信息

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM