C# 如何在数据库中输入成绩?
我不知道这是怎么回事,但它没有把测验成绩放在学生表上,有人能帮忙吗?这是我的密码C# 如何在数据库中输入成绩?,c#,database,C#,Database,我不知道这是怎么回事,但它没有把测验成绩放在学生表上,有人能帮忙吗?这是我的密码 private void button9_Click(object sender, EventArgs e) { int x = 0; if (textBox20.Text == "yours" || textBox20.Text == "Yours" || textBox20.Text == "YOURS") { x++;
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注入的内容。警告您的代码极易受到sql注入攻击!你不工作是什么意思?你调试代码了吗?你应该经常使用。这种类型的字符串连接对攻击是开放的。不要将密码存储为纯文本。对不起,我是个编程新手。这只是学校的项目,所以其实没什么大不了的。我只是不能把成绩存入数据库