我想在sql中写个判断,怎么去做呢?uid如果不为null的话,就传递uid进去
tid如果不为null的话,就传递tid进去
如果都为null的话,就不传递任何id进去
如果都不为null的话,就跳到error.aspx页面去
SqlCommand cmd = new SqlCommand("select * from Csga_Law_Class where type_id='"+ ??? +"'", con);能不能用一个表达式来做
而不是去判断参数然后做if……else if呢

解决方案 »

  1.   

    你想做多条件模糊查询?那就只能用拼字符串的形式了。看看PetShop 1.0的例子,那里面有一个。
      

  2.   

    如果都不为null的话,就跳到error.aspx页面去 
    跳转页面应该是无法用数据库实现的sql里可以做判断啊,就用if else 判断。。一个表达式搞定这些问题,不能把。。
      

  3.   

    一个表达式 + Try ... Catch ... 搞定
    应该再也没有更加简单的了,楼主给分吧... ...
    不过这样写代码,没什么必要
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                /*
                1.uid如果不为null的话,就传递uid进去 
                2.tid如果不为null的话,就传递tid进去 
                3.如果都为null的话,就不传递任何id进去 
                   这句有问题,因为不可能不传递,拼写字符串的时候不能传入NULL
                   因此理解楼主的需求是压根不需要"type_id=XX"这个条件
                4.如果都不为null的话,就跳到error.aspx页面去 
                */
                string uid = null;//string.Empty;
                string tid = null;//string.Empty;
                string sql = string.Empty;
                int a = 0;
                try
                {
                    //三元表达式  -__-~!! 汗.其实跟if ... else ...不就一样了?中间使用了1/0出错跳转..
                    sql = (uid != null & tid != null) ?
                                    (1 / a).ToString() : (
                                                                        uid = (uid != null) ?
                                                                                "select * from Csga_Law_Class where type_id='" + uid + "'" : (
                                                                                                tid = (tid != null) ?
                                                                                                       "select * from Csga_Law_Class where type_id='" + tid + "'"
                                                                                                       : "select * from Csga_Law_Class"
                                                                                                )
                                                                       );
                    ;
                    Console.WriteLine(sql);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Jump to error page!");
                }
                Console.ReadKey();
            }
        }
    }
      

  4.   

    绝对没有在语句中用"if……else if.."这种句法... 嘿嘿...
      

  5.   

    稍微修正一下,避免修改到UID TID的值
                   //三元表达式  -__-~!! 汗.其实跟if ... else ...不就一样了?中间使用了1/0出错跳转..
                    sql = (uid != null & tid != null) ?
                        (1 / a).ToString() : (
                                                sql = (uid != null) ?
                                                        "select * from Csga_Law_Class where type_id='" + uid + "'" : (
                                                                        sql = (tid != null) ?
                                                                               "select * from Csga_Law_Class where type_id='" + tid + "'"
                                                                               : "select * from Csga_Law_Class"
                                                                                )
                                                           );
                    ;
      

  6.   

    呵呵,几位都是在c#中处理逻辑的
    可是楼主想在sql中判断不知道楼主是个啥意思
      

  7.   

    CASE WHEN
      

  8.   

    在SQL中判断,可使用存储过程,传递值到存储过程判断
    还可使用三元操作符
    string sql=""; 
    sql="select * from Csga_law where 1=1"; 
    sql+=(uid==null?"":" and type_id='"+uid+"'");
    sql+=(tid==null?"":" and type_id='"+tid+"'");
      

  9.   

    又是LS正解,我就是不知道这个怎么去用
    谁会去再sql中做判断呢,呵呵
      

  10.   

    在sql中用case when 来判断,选择字段的值也可以啊
      

  11.   

    declare @uid varchar(100)
    ...
    select * from tb where (uid=@uid or @uid is null) or(nid=@nid or @nid is null)...