简体   繁体   English

SQL 更新查询不更新表数据

[英]SQL update query not updating table data

Here is my code and it shows successful but it does not update row in table.这是我的代码,它显示成功,但没有更新表中的行。

cm = new SqlCommand("update table_products set pname = @pname, pdesc = @pdesc, bid = @bid, cid = @cid, cost = @cost, price = @price, reorder = @reorder where pcode like @pcode", con);

cm.Parameters.AddWithValue("@pcode", tft_pcode.Text);
cm.Parameters.AddWithValue("@barcode", tft_barcode.Text);
cm.Parameters.AddWithValue("@pname", tft_pname.Text);
cm.Parameters.AddWithValue("@pdesc", tft_pdescription.Text);
cm.Parameters.AddWithValue("@bid", bid);
cm.Parameters.AddWithValue("@cid", cid);
cm.Parameters.AddWithValue("@cost", Double.Parse(tft_cost.Text));
cm.Parameters.AddWithValue("@price", Double.Parse(tft_price.Text));
cm.Parameters.AddWithValue("@reorder", int.Parse(tft_reorder.Text));

cm.ExecuteNonQuery();
con.Close();

MessageBox.Show("Product updated successfully");

cm.ExecuteNonQuery(); returns the number of rows affected.返回受影响的行数。 In your case it's probably 0 (but still worth getting the value and checking).在您的情况下,它可能为 0(但仍然值得获得价值并检查)。

In your case, it sounds like where pcode like @pcode on the end of your query, is not matching anything in the table.在您的情况下,听起来像查询末尾的where pcode like @pcode表中的任何内容都不匹配。

var rowsUpdated = cm.ExecuteNonQuery();
con.Close();

if(rowsUpdate>0) MessageBox.Show("Product updated successfully");
else MessageBox.Show("Product NOT updated successfully");

If you're using a file based database like a sql server mdf file that is being dynamically attached to your db server when you run your project you should be aware that the db your program is updating is not necessarily the same file you're looking in with your management studio.如果您使用的是基于文件的数据库,例如 sql 服务器 mdf 文件,该文件在您运行项目时动态附加到您的数据库服务器,您应该知道您的程序正在更新的数据库不一定是您正在寻找的同一个文件在你的管理工作室。 Take a look at your connection string- if it mentions DataDirectory then it's likely it's the database file in the bin/Debug or bin/Release (depending on your active build config) folder that is being updated, not the one in your project folder.查看您的连接字符串 - 如果它提到 DataDirectory,那么很可能是正在更新的 bin/Debug 或 bin/Release(取决于您的活动构建配置)文件夹中的数据库文件,而不是项目文件夹中的那个。 The db in your project folder is copied out to bin/* every tine you run your project.项目文件夹中的 db 被复制到 bin/* 您运行项目的每个 tine 中。 This has classically been an awesome cause of "my program says it updates my db but it doesn't" - either the db is being replaced with a clean one every time the program is run or the dev is looking in one db file and the program is updating another这通常是“我的程序说它更新了我的数据库但它没有”的一个很棒的原因 - 每次运行程序时数据库都被替换为干净的数据库,或者开发人员正在查看一个数据库文件并且程序正在更新另一个

I solved the issue The problem was that i was trying to match pcode(application) with pcode(database), which wasn't doing so.我解决了这个问题问题是我试图将 pcode(application) 与 pcode(database) 匹配,但没有这样做。 That was because when my update form was loading i had the pcode generate method in its event, so i was matching with old pcode value, what it actually was doing is that, it was matching with newly generated pcode.那是因为当我的更新表单加载时,我的事件中有 pcode generate 方法,所以我匹配旧的 pcode 值,它实际上正在做的是,它与新生成的 pcode 匹配。 So i removed pcode generate method from form_load event and the issue was solved.所以我从 form_load 事件中删除了 pcode generate 方法,问题就解决了。 Thank you All for your time.谢谢大家的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM