仅在选定列上更新SQL(Update SQL only on selected columns)

编程入门 行业动态 更新时间:2024-10-17 15:31:28
仅在选定列上更新SQL(Update SQL only on selected columns)

我在C#中构建编辑菜单以编辑数据库中的SQL行,但我希望用户能够选择要更新的特定行中的哪一列,如果他们将框保留为空则更新我不想更新该列。 例如

用户看到三个框,他可以在一个框中输入一些内容,这反映出只更新行中的一列,其余的列将保持不变。 我开始实现这样的东西

string RowUpdate =""; if(Title !="" && Author =="" && Pages==""){ RowUpdate = "Update BookTable set Title=@Title where BookID=@ID"; } else if (Title =="" && Author !="" && Pages ==""){ RowUpdate = "Update BookTable set Author=@Author where BookID=@ID"; }

当逻辑完成时,将选择正确的SQL,因为您可以看到只有3个文本框的这种类型的逻辑会产生9种不同的结果,并且随着编辑行的增长它会呈指数级缩放。

我不知道是否可能,但如果没有输入值,有没有办法更新唯一的列,所以我不必编写巨大的逻辑来更新特定的列?

就像是

RowUpdate = "Update BookTable set (IF null Skip Title=@Title),(If null Skip Author=@Author), (If null Skip Pages=@Pages) where BookID=@ID

或任何其他方式来做到这一点更简单,更快捷? 谢谢你的回答

I building edit menu in C# to edit SQL rows in the database but I want the user to be able to choose which column in the particular row to update to update if they left the box empty I don't want to update that column. for example

the user sees three boxes he can enter something into one box that would reflect updating only one column in the row, rest of the columns would stay as they were. I started implementing something like this

string RowUpdate =""; if(Title !="" && Author =="" && Pages==""){ RowUpdate = "Update BookTable set Title=@Title where BookID=@ID"; } else if (Title =="" && Author !="" && Pages ==""){ RowUpdate = "Update BookTable set Author=@Author where BookID=@ID"; }

when logic is completed the Correct SQL will be picked, as you can see this type of logic for only 3 text boxes take 9 different outcomes and it scales exponentially as edit rows would grow.

I don't know if that even possible but is there a way to update an only particular column if there was no value entered so I don't have to write huge logic to update only particular column?

something like

RowUpdate = "Update BookTable set (IF null Skip Title=@Title),(If null Skip Author=@Author), (If null Skip Pages=@Pages) where BookID=@ID

or any other way to do this simpler and quicker? thank you for answers

最满意答案

这不是最好的解决方案,但希望它能满足您的需求。 它将根据可用值创建查询。

string rowUpdate = "UPDATE BookTable SET "; if(Table != "") rowUpdate += "Table=@table "; if(Author != "") rowUpdate += "Author=@author "; . . rowUpdate += "WHERE BookID=@ID ";

This is not the best solution, but hope that it will fulfill what you need to do. It will create query on the basis of value available.

string rowUpdate = "UPDATE BookTable SET "; if(Table != "") rowUpdate += "Table=@table "; if(Author != "") rowUpdate += "Author=@author "; . . rowUpdate += "WHERE BookID=@ID ";

更多推荐

本文发布于:2023-08-06 18:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1454524.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Update   SQL   columns   selected

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!