这是我的程序应该有3列更新表中的分数:id
,nickname
,scor
。
Otherform.id1
是id
需要更新的用户的身份,它是从登录表单中获取的。
void update()
{
SqlConnection c = new SqlConnection(co);
c.Open();
string up1 = "Update Scoruri Set Scor='" + Convert.ToInt32(scor1) + "' where ID='" + otherForm.id1 + ";'";
//z.Parameters.AddWithValue("@y", scor1);
SqlCommand comm = new SqlCommand(up1, c);
comm.ExecuteNonQuery();
string up2= "'Update Scoruri Set scor='" +Convert.ToInt32(scor2) + "' where ID='" + otherForm.id2 + "';'";
SqlCommand h = new SqlCommand(up1, c);
h.ExecuteNonQuery();
c.Close();
}
您没有确切地说出问题是什么,但是我发现您的代码中有一些错误。
Scor
和ID
列是数字,而不是字符。这就是为什么您不应该在其中使用单引号。使用参数化查询,您无需担心。using
语句来处置您的SqlConnection
adn SqlCommand
。SqlCommand
即h
)中,您正在执行第一个命令(up1
),而不是第二个命令(up2
)。举个例子;
void Update()
{
using(SqlConnection c = new SqlConnection(co))
using(SqlCommand comm = c.CreateCommand())
{
string up1 = "Update Scoruri Set Scor=@scor where ID=@id";
comm.CommandText = up1;
comm.Parameters.AddWithValue("@scor", Convert.ToInt32(scor1));
comm.Parameters.AddWithValue("@id", otherForm.id1);
c.Open();
comm.ExecuteNonQuery();
comm.Parameters.Clear();
comm.Parameters.AddWithValue("@scor", Convert.ToInt32(scor2));
comm.Parameters.AddWithValue("@id", otherForm.id2);
comm.ExecuteNonQuery();
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句