使用带有乘数函数的 dataadapter 编写更新 SQL 查询
我有一个使用 C# 的 Windows 应用程序。我正在使用 Northwind 产品表,我需要按相同的百分比更新所有单价。我知道如何在 SELECT 查询中编写此内容,但在如何使用乘法运算符和 sql 参数以及 UPDATE 查询表单类编写更新到我的数据访问层时遇到麻烦,它显示在 SQL 查询中,但我没有放置它在正确的位置,因为我得到了空。谁能帮我如何编写更新字符串?这是我正在努力解决的 SQL 部分的片段:
//Update using SQL string
productDataAdapter.UpdateCommand.CommandText =
"UPDATE Products SET " +
"UnitPrice * " + multiplier = "@UnitPrice";
holdParm = new SqlParameter();
holdParm.ParameterName = "@UnitPrice";
holdParm.SourceColumn = "UnitPrice";
productDataAdapter.UpdateCommand.Parameters.Add(holdParm);
//Open connection
productDataAdapter.InsertCommand.Connection.Open();
//usd data adapter to update the Products table
rowCount = productDataAdapter.Update(productsDataSet, "Products");
return rowCount;
I have a windows application using c#. I am using Northwind Products table and I need to update all Unit Prices by the same percentage. I know how to write this in a SELECT query but having trouble with how to write update using multiplication operator and sql parameters and UPDATE query form class to my data access layer and it shows up in the SQL query but I don't have it placed in the right spot because I get null. Can anyone help me with how to write the update string? Here's a snippet of the part of SQL I'm struggling with:
//Update using SQL string
productDataAdapter.UpdateCommand.CommandText =
"UPDATE Products SET " +
"UnitPrice * " + multiplier = "@UnitPrice";
holdParm = new SqlParameter();
holdParm.ParameterName = "@UnitPrice";
holdParm.SourceColumn = "UnitPrice";
productDataAdapter.UpdateCommand.Parameters.Add(holdParm);
//Open connection
productDataAdapter.InsertCommand.Connection.Open();
//usd data adapter to update the Products table
rowCount = productDataAdapter.Update(productsDataSet, "Products");
return rowCount;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只执行查询怎么样?
然后使用 dbCommand 对象,按如下方式设置:
这样您就可以运行 UPDATE 并且仅运行更新,并且您知道它的作用以及何时发生。当然,根据需要过滤 UPDATE,以便您只更新您想要更新的内容。
How about just execute the query?
Then use the dbCommand object, set it as follows:
That way you're running the UPDATE and only the update and you know what it does and when it happens. Of course, filter that UPDATE as needed so that you only update what you mean to update.
您正在以非标准方式使用 DataAdapter。我建议您考虑两种不同的方法:
当您使用 DataAdapter 将数据保存回数据库时,我看不出进行转换有任何优势。我发现这种方法的一个缺点是,对于习惯使用 DataAdatpers 来简单地加载和保存数据的程序员来说,它会让他们感到困惑。
You are using a DataAdapter in a non-standard way. I'd suggest you consider 2 different approaches:
I can't see any advantage to doing a transformation while you're saving the data back to the database using the DataAdapter. One downside I can see with this approach is that it will be confusing to programmers who are used to using DataAdatpers to simply load and save data.