这是我的程序,应该更新包含 3 列的表中的分数:id
, nickname
, scor
.
Otherform.id1
is the 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();
}
您没有准确说出问题所在,但我发现您的代码中有一些问题。
- 你应该总是使用参数化查询 http://blog.codinghorror.com/give-me-parameterized-sql-or-give-me-death/。这种字符串连接是开放的SQL注入 http://en.wikipedia.org/wiki/SQL_injection攻击。
- 我假设你的
Scor
and ID
列是数字,而不是字符。这就是为什么你不应该使用单引号。使用参数化查询,您无需担心这一点。
- Use using陈述 http://msdn.microsoft.com/en-us/library/yh598w02.aspx处置你的
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)