用户检查/取消选中treeview节点时,重新创建SQL SELECT查询 [英] Recreate SQL SELECT query when user checks/unchecks treeview node
本文介绍了用户检查/取消选中treeview节点时,重新创建SQL SELECT查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
介绍和相关信息:
在开始之前,重要的是介绍示例SQL表将在这个问题中用来说明我面临的问题:
INTRODUCTION AND RELEVANT INFORMATION:
Before I start, it is important to introduce example SQL table that will be used in this question to illustrate the problems I face:
Table < #ID autonumber, Planet varchar(20) >
填充了一些记录的SQL表如下所示:
SQL table populated with some records looks like below:
ID | Planet
1 | Mercury
2 | Venus
3 | Earth
Treeview填充以下模式:
- 父node = column name
- Parent的子项=不同的列值
- 如果用户检查 Mercury ,我的查询类似于
表中的SELECT ID,其中Planet ='Mercury';
- 如果用户检查多个节点,例如 Mercury 和地球然后我的查询将成为
表中的SELECT ID,其中行星IN('Mercury','Earth') ;
。 - if user checks Mercury, my query would be something like
SELECT ID from Table where Planet = 'Mercury';
- if user checks multiple nodes, for example Mercury and Earth then my query would become
SELECT ID from Table where Planet IN ( 'Mercury', 'Earth' );
.
推荐答案
(图片链接似乎不起作用,所以我无法与TreeView对话。)
使用参数化语句;没有必要全力以赴。
如果您使用的是SQL Server,那么还要查看表值参数。
SQL Server 2008用户定义的表类型和表值参数 [ ^ ]
您也可以考虑使用行星ID的位图(从不使用自动编号或身份来获取简单/静态数据)比如这个):
(The image link didn't seem to work, so I can't speak to the TreeView.)
Use a parameterized statement; there is no need to go all dynamic on this.
If you are using SQL Server, then also look into Table-Valued Parameters.
SQL Server 2008 User Defined Table Types and Table-Valued Parameters[^]
You might also look into using bitmaps for the planet IDs (never use autonumbers or identities for simple/static data such as this):
ID | Planet
1 | Mercury
2 | Venus
4 | Earth
8 | Mars
...
然后你可以在代码端有一个枚举并传递一个位映射(整数)价值,例如值12表示地球和火星。
这具有不需要在SQL中进行字符串比较的附加值。
You can then have an enumeration on the code side and pass one bit-mapped (integer) value, e.g. value 12 means "Earth and Mars".
This has the additional value of not requiring string comparisons in the SQL.
这篇关于用户检查/取消选中treeview节点时,重新创建SQL SELECT查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文