sql语句拼接

        在组合查询是怎样用一个简单的方法,判断局部控件是否为空呢?

   if (cmbrelation1.Text == "") //第一个逻辑符号为空
            {
                Control[] arrcontrol = new Control[3];
                arrcontrol[0] = cmbfields1;//第一行文本框的名字
                arrcontrol[1] = cmboperator1;
                arrcontrol[2] = txtcontext1;
                //调用函数判断控件内容是否为空
                bool falg;
                falg = issomeempty(arrcontrol);
                if (falg == true)
                {
                    MessageBox.Show("请将第一行信息填写完整");
                    enGroupQuery = null;
                    return;
                }
                enGroupQuery.debug = 1;
            }


       用这个方法就可以了。

 

public bool issomeempty(Control[] arrcontrol)
        {
            bool flag;
            int i = 0;
            foreach (Control item in arrcontrol)
            {
                if (item.Text == string.Empty)
                {
                    i += 1;
                }
                else
                {
                }
            }
            if (i > 0)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            return flag;
        }

     组合查询时,我们把所有的信息用一个实体类装好,传递给d层

    

 //将参数传递给实体,调用重新的方法
            enGroupQuery.fileds1 = ToName(cmbfields1.Text);
            enGroupQuery.fileds2 = ToName(cmbfields2.Text);
            enGroupQuery.fileds3 = ToName(cmbfields3.Text);

            enGroupQuery.operator1 = cmboperator1.Text;
            enGroupQuery.operator2 = cmboperator2.Text;
            enGroupQuery.operator3 = cmboperator3.Text;

            enGroupQuery.context1 = txtcontext1.Text.Trim();
            enGroupQuery.context2 = txtcontext2.Text.Trim();
            enGroupQuery.context3 = txtcontext3.Text.Trim();

            enGroupQuery.relation1 = ToName(cmbrelation1.Text);
            enGroupQuery.relation2 = ToName(cmbrelation2.Text);

            // enGroupQuery.table = Getdtable();
            enGroupQuery.table = Getdtable();
            if (enGroupQuery == null)
            {
                return;
            }
            else
            {
                //调用外观层
                Facade.GroupWorkFacade groupworkuser = new Facade.GroupWorkFacade();
                DataTable table = groupworkuser.Groupworkway(enGroupQuery);


        然后返回的table就组合查询得到的信息了,不论是否所有的信息都填写完整,都赋值传递到D层。其中加了一个属性debug。

       在局部判空中,如果不为空,就赋值debug。

    尝试很多次用存储过程。但是没有成功,SQL语句只能在D层写了。以前在vb机房中,只是按着葫芦画瓢,不懂+“”+ 这个是什么意思,空不是什么都没有吗?原来不加空,有会使两个独立的单词合到一起。

      还有就是 string str = "select * from A where B=' " + value + " ' ";   ‘  “  + value +  ” ’  这样写是什么意思?这里value代表一个变量,如果直接在字符串中写变量,就会化成字符串了,我是刚刚才懂。

     调试,可视化文本,非常好用,大赞。我为什么刚刚知道它的好处?

     看一下D层。

  public DataTable  groupworkway(Entity.GroupQuery engroupquery)
        {
            string sql;
            SQLHeper.sqlhelper sqlhelper = new SQLHeper.sqlhelper();//创建一个sqlHeper类对象
            SqlParameter[] sqlParams = { new SqlParameter("@fileds1",engroupquery.fileds1 ),
                                         new SqlParameter("@fileds2",engroupquery.fileds2 ),
                                         new SqlParameter("@fileds3",engroupquery.fileds3 ),
                                         new SqlParameter("@operator1",engroupquery.operator1 ),
                                         new SqlParameter("@operator2",engroupquery.operator2 ),
                                         new SqlParameter("@operator3",engroupquery.operator3 ),
                                         new SqlParameter("@context1",engroupquery.context1 ),
                                         new SqlParameter("@context2",engroupquery.context2),
                                         new SqlParameter("@context3",engroupquery.context3),
                                         new SqlParameter("@relation1",engroupquery.relation1 ),
                                         new SqlParameter("@relation2",engroupquery.relation2 ),
                                         new SqlParameter("@table",engroupquery.table )

                                       };
            string strl = engroupquery.table;
            if (engroupquery.debug==1)
            {
                sql = @"SELECT * FROM " + engroupquery.table + " WHERE " + engroupquery.fileds1 + engroupquery.operator1 + "@context1";
                //string str = "select * from A where B=' " + value + " ' ";
                //sql = @"SELECT * FROM " + strl + " WHERE @fileds1 + '" + engroupquery.operator1 + "'+ @context1";
                   
                 DataTable table = SQLHeper.sqlhelper.GetDataTable(sql, CommandType.Text, sqlParams);

                 return table;
            }
            if (engroupquery.debug==2)
            {
                sql = @"SELECT * FROM " + strl + " WHERE " + engroupquery.fileds1 + engroupquery.operator1 + "@context1 " + engroupquery.relation1 + " " + engroupquery.fileds2 + engroupquery.operator2 + "@context2";
               // sql = sql + engroupquery.relation1 + engroupquery.fileds2 + engroupquery.operator2 + "@context2";
                DataTable table = SQLHeper.sqlhelper.GetDataTable(sql, CommandType.Text, sqlParams);

                return table;
            }
            if (engroupquery.debug==3)
            {
                 sql = @"SELECT * FROM " + strl + " WHERE " + engroupquery.fileds1 + engroupquery.operator1 + "@context1 " + engroupquery.relation1 + " " + engroupquery.fileds2 + engroupquery.operator2 + "@context2";
                 sql += " " + engroupquery.relation2 + " " + engroupquery.fileds3 + engroupquery.operator3 + "@context3";
                 DataTable table = SQLHeper.sqlhelper.GetDataTable(sql, CommandType.Text, sqlParams);
                 return table;
            }
            else
            {
                return null;
            }
          
            
        }


    

 

  

 

         

 

评论 25
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值