C# sql update命令不更新数据库
我有这个按钮根据程序中给定的ID更新一个空表单元格 代码没有错误,但不更新表 在C# sql update命令不更新数据库,c#,sql,C#,Sql,我有这个按钮根据程序中给定的ID更新一个空表单元格 代码没有错误,但不更新表 在 cmd4.Parameters.AddWithValue("@id",ActiveAthNum); 我试着将其保留为字符串,并将其转换为int。表单元格的ID和Points都是int AfRows返回0。。。但是查询本身在SQL中运行得很好。。。 ActiveAthNum和PointsGiven变量具有它们应该具有的值 代码: private void btnSavePoints_Click(object sen
cmd4.Parameters.AddWithValue("@id",ActiveAthNum);
我试着将其保留为字符串,并将其转换为int。表单元格的ID和Points都是int
AfRows返回0。。。但是查询本身在SQL中运行得很好。。。
ActiveAthNum和PointsGiven变量具有它们应该具有的值
代码:
private void btnSavePoints_Click(object sender, EventArgs e)
{
con = new SqlConnection(CONNSTRING);
cmd4 = new SqlCommand("UPDATE SadaTestTable SET Points = @pts WHERE id = @id");
cmd4.Parameters.AddWithValue("@id", ActiveAthNum);
PointsGiven = Convert.ToInt32(tbPointsGiven.Text);
cmd4.Parameters.AddWithValue("@pts", PointsGiven);
cmd4.Connection = con;
int AfRows;
con.Open();
AfRows = cmd4.ExecuteNonQuery();
}
问题可能出在ID参数的类型上。请尝试指定正确的数据库类型:
cmd4.Parameters.Add("id", SqlDbType.Char); //set proper type as in table
cmd4.Parameters["id"].Value = "";
你真的应该把那个DB调用放到Try…Catch…Finally块中 数据库的默认语言是什么?很久以前,我看到这种类型的错误没有更新,但是当DB语言设置为US-EN而不是数据库默认值时,没有错误 DB会抛出一个关于更改默认语言的错误,但它不会在我的ASP代码中触发错误。关于错误代码的某些内容没有注册为错误-我必须为特定的错误号编码才能绕过它。我只见过两次,但在两个不同的公司…问题在于您没有指定数据类型
cmd4.Parameters.AddWithValue("@id", SqlDbType.Int).Value = ActiveAthNum;
id列的类型是什么?ActiveAthNum的类型和值是什么?当然,不要使用.AddWithValue。检查@id值。您传递的值是否正确?id列为Int Points也是Int,ActiveAthNum在之前的代码中被强制转换为Int,当它被赋予值时,您可以演示如何将ActiveAthNum强制转换为Int。如果它是字符串,您应该将其解析为Int。@frcake添加参数值时,您的cmd4是什么样子的?调试代码并告诉我们.cmd4.Parameters.Addid,SqlDbType.Char.Value=;:
private void btnSavePoints_Click(object sender, EventArgs e)
{
con = new SqlConnection(CONNSTRING);
cmd4 = new SqlCommand("UPDATE SadaTestTable SET Points = @pts WHERE id = @id",con);
cmd4.Parameters.AddWithValue("@id", Datatype.Char);//if the datatype in the database is char but if the data type in the database is int then is datatype.int
PointsGiven = Convert.ToInt32(tbPointsGiven.Text);
cmd4.Parameters.AddWithValue("@pts", PointsGiven);
try
{
con.open();//here open my connection
cmd4.connection = con;
Afrows = cmd4.ExecutenonQuery();//Execute my query
}
catch
{
throw;//possible exceptions in the system
}
finally
{
con.Close();//closing connections
}
}