更新SQL数据库数据文件C#后告诉它已更新。我检查了SQL数据文件。它没有更新 [英] After updating SQL databse datafile C# telling it is updated. I checked in SQL data file. It is not updated

查看:85
本文介绍了更新SQL数据库数据文件C#后告诉它已更新。我检查了SQL数据文件。它没有更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码。有人可以告诉我搞砸了什么和哪里。



Here is my code. someone can tell what and where I messed.

con = new SqlConnection(@"Data Source=.\SQL2016;Initial Catalog=inventoryDB;Integrated Security=True");
con.Open();//opened connection
//here updating records and saving to my sql datafile.
cmd = new SqlCommand("UPDATE itemDB SET brname=@label16, itmname=@label4, itmunit=@label6, itmgr=@label14 ,itmmrp=@label2, itmbyp=@label9,itmdlrp=@label10, itmtx=@label8, itmdlrmrg=@label12, itmrtmrg=@label13, itmusrcode=@label7, active=@label3 WHERE itmcode=@label17", con);
cmd.Parameters.Add("@label16", ncombobrnd.GetItemText(ncombobrnd.SelectedIndex));
int num = 0;
MessageBox.Show(num.ToString());
cmd.Parameters.Add("@label4", itemname.Text);
cmd.Parameters.Add("@label6", combouom.GetItemText(combouom.SelectedItem));
cmd.Parameters.Add("@label14", itemkgs.Text);
cmd.Parameters.Add("@label2", itemmrp.Text);
cmd.Parameters.Add("@label9", itembrp.Text);
cmd.Parameters.Add("@label10", itemslp.Text);
cmd.Parameters.Add("@label8", itemtax.Text);
cmd.Parameters.Add("@label12", itemdlmargin.Text);
cmd.Parameters.Add("@label13", itemretailmargin.Text);
cmd.Parameters.Add("@label7", itemcode.Text);
cmd.Parameters.Add("@label3", status);
cmd.Parameters.Add("@label17", recbox.Text);
cmd.ExecuteNonQuery();
combobrand.SelectedIndex = -1;
combouom.SelectedIndex = -1;
comboactive.SelectedIndex = -1;
itemname.Text = "";
itemunit.Text = "";
itemkgs.Text = "";
itemltrs.Text = "";
itemtax.Text = "";
itemmrp.Text = "";
itembrp.Text = "";
itemslp.Text = "";
itemtax.Text = "";
itemdlmargin.Text = "";
itemretailmargin.Text = "";
itemcode.Text = "";
MessageBox.Show("Record Updated", "Updated", MessageBoxButtons.OK, MessageBoxIcon.Information);
con.Close();
dataGridView1.Rows.Clear();
dataload(); // to refresh the data and display i





我尝试过:



我试图解决但我失败了。我不知道我做了什么错误以及在哪里。



What I have tried:

I tried to solve but I failed. I dont know what mistake I did and where.

推荐答案

它告诉你它已经更新,因为你正在显示一条带有该消息的消息框。但你没有检查更新是否成功。



要做到这一点,请检查SqlCommand.ExecuteNonQuery Method(System.Data.SqlClient) [ ^ ]返回值表示受影响的行数。



我想这会在你的情况下返回零。



有些怀疑:

您选择按项目代码选择记录(WHERE itmcode = @ label17)但传递具有不同名称的参数:

It is telling you that it has been updated because you are showing a message box with that message. But you did not check if the update was successful.

To do this check the SqlCommand.ExecuteNonQuery Method (System.Data.SqlClient)[^] return value which indicates how many rows has been affected.

I guess that this returns zero in your case.

There is something suspipcious:
You are selecting selecting the record by itemcode ("WHERE itmcode=@label17") but passing parameters with different name:
cmd.Parameters.Add("@label7", itemcode.Text);
cmd.Parameters.Add("@label17", recbox.Text);

我希望 @ label17 已设置为 itemcode



如果这不是您的错误来源,您必须深入查询并检查它是否有效。但只有你可以这样做,因为我们没有你的数据库。

I would expect that @label17 has be set to itemcode.

If this is not the source of your error, you have to dig into your query and check if it is valid. But only you can do that because we did not have your database.


这篇关于更新SQL数据库数据文件C#后告诉它已更新。我检查了SQL数据文件。它没有更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆