SQLCommand 中的更新语句不起作用
我有以下应该执行更新的 SQLCommand。问题是,我没有收到错误,但它仍然不更新数据库?
SqlConnection sqlconn2 = new SqlConnection(this.connectionString);
sqlconn2.Open();
string strCmd = "UPDATE dbo.mydata SET WEB_OBEZ1 = @OBEZ1, WEB_OBEZ2 = @OBEZ2, WEB_OBEZ3 = @OBEZ3 WHERE O_KURZ = @OKURZ";
using (SqlCommand cmd2 = new SqlCommand(strCmd, sqlconn2))
{
cmd2.Parameters.Add("@OBEZ1", SqlDbType.NVarChar);
cmd2.Parameters.Add("@OBEZ2", SqlDbType.NVarChar);
cmd2.Parameters.Add("@OBEZ3", SqlDbType.NVarChar);
cmd2.Parameters.Add("@OKURZ", SqlDbType.NVarChar);
foreach (DataRow dr in dt.Rows)
{
// Felder holen
string okuerzel = dr["O_KURZ"].ToString();
string bezeichnung = dr["O_BEZ"].ToString();
string[] lines = CreateNewOrgBez(bezeichnung);
cmd2.Parameters["@OBEZ1"].Value = lines[0];
cmd2.Parameters["@OBEZ2"].Value = lines[1];
cmd2.Parameters["@OBEZ3"].Value = lines[2];
cmd2.Parameters["@OKURZ"].Value = okuerzel;
cmd2.ExecuteNonQuery();
}
}
sqlconn2.Close();
I have the following SQLCommand which should do an Update. The issue is, that I get no errors but it still do not update the database?
SqlConnection sqlconn2 = new SqlConnection(this.connectionString);
sqlconn2.Open();
string strCmd = "UPDATE dbo.mydata SET WEB_OBEZ1 = @OBEZ1, WEB_OBEZ2 = @OBEZ2, WEB_OBEZ3 = @OBEZ3 WHERE O_KURZ = @OKURZ";
using (SqlCommand cmd2 = new SqlCommand(strCmd, sqlconn2))
{
cmd2.Parameters.Add("@OBEZ1", SqlDbType.NVarChar);
cmd2.Parameters.Add("@OBEZ2", SqlDbType.NVarChar);
cmd2.Parameters.Add("@OBEZ3", SqlDbType.NVarChar);
cmd2.Parameters.Add("@OKURZ", SqlDbType.NVarChar);
foreach (DataRow dr in dt.Rows)
{
// Felder holen
string okuerzel = dr["O_KURZ"].ToString();
string bezeichnung = dr["O_BEZ"].ToString();
string[] lines = CreateNewOrgBez(bezeichnung);
cmd2.Parameters["@OBEZ1"].Value = lines[0];
cmd2.Parameters["@OBEZ2"].Value = lines[1];
cmd2.Parameters["@OBEZ3"].Value = lines[2];
cmd2.Parameters["@OKURZ"].Value = okuerzel;
cmd2.ExecuteNonQuery();
}
}
sqlconn2.Close();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许 dt.Rows 的长度为零。你检查过这个吗?
Perhaps the length of
dt.Rows
is zero. have you checked for this?使用交易?那么也许缺少一个提交?
马里奥
using transactions? then perhapse a commit is missing?
hth
Mario
也许 where 子句与数据库中的任何行都不匹配?
ExecuteNonQuery() 返回受影响的行数,检查一下!
马里奥
Perhapse also the where clause did not match any row in the db?
ExecuteNonQuery() returns the number of rows affected, check it!
hth
Mario