机房收费系统之组合查询的传参问题

来源:互联网 发布:电子地图软件开发 编辑:程序博客网 时间:2024/05/16 18:18
         我的机房收费系统中,我研究最多的就是这个组合查询了,因为第一次做的时候,没有做出来这样的组合查询,而是省时做了点简单的查询。这篇我想说一下我的传参问题。 

         最开始的时候,我用的是sql语句传参的方式写的,sql语句是这样写的(其中basicStudentInfo.FileName=cmbFieldName.SelectIndex,即实现了查询字段与下拉框选项的Index动态组合):

"select * from CardInfo,StudentInfowhere crdstudentno=stdstudentno and @FileName1" & _                        basicStudnetInfo1.strOperator & "@value1 " & " " _& basicStudnetInfo1.strRelation & " " & "@FileName2" & basicStudentInfo2.strOperator & "@value2" & " " _& basicStudentInfo2.strRelation & " " & "@FileName3" & basicStudentInfo3.strOperator & "@value3"



 

         其中basicStudentInfo是我定义的类,在我传参的时候,我将@FileName传入的是basicStudentInfo.FileName.tostring类型是char型。可是问题出来了,就是查不出来结果。然后我将@FileName参数去掉,直接采用的拼接字符串,直接写上basicStudentInfo.FileName.tostring,然后就出来结果了。刚开始我怎么也想不出来结果,然后就先放着了。

"select *from CardInfo,StudentInfo where crdstudentno=stdstudentno and " & _ basicStudnetInfo1.fieldName.ToString & basicStudnetInfo1.strOperator & "@value1 " & " " _& basicStudnetInfo1.strRelation & " " & basicStudentInfo2.fieldName.ToString & basicStudentInfo2.strOperator & "@value2" & " " _& basicStudentInfo2.strRelation & " " & basicStudentInfo3.fieldName.ToString & basicStudentInfo3.strOperator & "@value3"


 

 

         第二天看英语的时候,突然想到,这个表里的字段名写在sql语句里面应该是stdstudentno之类的词语,可是如果像我这样传参的话,那么查询的字段名就会变成'stdstudentno’,也就是'stdstudentno'='卡号'所以造成了我查不出来结果这件事情。

         印象中听过杨元说将表名作为参数传入,那么就会碰到和这个一样的问题。然后向他请教了一下。

         解决方法是这样的,存储过程中接收了这些参数后,然后在存储过程中定义一个字符类型的临时变量,在存储过程中拼接这些字符串,如果这样的话,那么四个组合查询就可以都用这一个存储过程了,那就是在传参的时候,多传一个表名进来就可以了。

         上面就是我在组合查询的过程中遇到的传参问题。

         总的来说,我的组合查询中出去界面空值判断的地方,我只在判断relation除用到了选择判断语句,通过大话中的小例子,除去选择判断语句有利于扩展,所以在遇到选择判断语句时,我总是先想办法避免,想不出来的时候再先写上去。

         我十分信奉一句话,每个人一生吃的苦的和是一定的。所以每想到一个简单的方法,那么就会遇到别的问题,例如在只有一个条件的时候,我必须去查三个条件的and关系。所以没有哪种方法是简单的,只有最适合的。