动态更新查询 [英] Dynamic Update Query

查看:61
本文介绍了动态更新查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库中有数据,我可以在列表视图中查看。我有一个更新功能,当我单击列表视图数据的单行时,它将自动连接到我的数据库,以便当我在文本框中输入值并按更新按钮时,它将更新数据。现在的问题是我无法更新数据,除非填写了所有文本框。我想要一个动态更新按钮,如果我有姓名,地址,年龄和邮政编码。然后我只想更新地址,我只会输入地址文本框,然后按更新按钮更新数据的地址栏。

I have data in my database and i can view it in my list view. I have an update feature that when i click a single row of the listview data it will automatically connect to my database so that when i enter values in the textbox and press update button it will update the data. Now the problem here is that I can't update the data unless all the text boxes are filled in. I want a dynamic update button where if i have like name, address, age and zip code. then i just want to update the address, i will only type in the address textbox then press the update button to update the address column of the data only.

推荐答案

全部你需要做的是在更新时检查有数据的参数。



例如:

All you need to do is check for the parameter having data when doing your update.

For example:
UPDATE Table1
SET field1 = CASE WHEN @field1 IS NULL THEN field1 ELSE @field1 END, 
  field2 = CASE WHEN @field2 IS NULL THEN field2 ELSE @field2 END, 





如果为@ field1参数传递NULL,这将使field1单独使用。等等。然后在你的C#中,如果他们没有改变列你为参数传递NULL。



This will leave field1 alone if you pass NULL in for the @field1 parameter. Etc. Then in your C# if they did not change the column you pass NULL for the parameter.


Ryan Dev的解决方案将在这里工作,除了小的改变是检查空值你可以检查如下。





Ryan Dev's solution will work here except small change is checking for blank value you can check like below.


UPDATE Table1
  SET
    field1 = CASE WHEN @field1 IS NULL OR @field1 = '' THEN field1 ELSE @field1 END,
    field2 = CASE WHEN @field2 IS NULL THEN field2 OR @field2 = '' ELSE @field2 END
  where
    pk = @pk


这篇关于动态更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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