我创建了8个TextBox,我要在其中填入任意几个信息,都可以从同一张数据表找到相应的数据,并在gridview中显示

解决方案 »

  1.   

    都在的呀,数据库里都有数据的,只是这是不确定的查找,where条件不确定啊
      

  2.   

    楼上的  只能拼接sql  你这样 string sWhere=string.Empty; if(string.IsNullOrEmpty(textbox.text)){ if(sWhere!=''){sWhere+="AND"}Swhere+="条件"  }if(string.IsNullOrEmpty(textbox1.text)){ if(sWhere!=''){sWhere+="AND"}Swhere+="条件1"  }  当然 特殊字符要过滤的 还有就是想简单 不想写 if(sWhere!=''){sWhere+="AND"} 可以声明的时候写string sWhere=”1=1“  当然下面拼sql 你应该会的 注意过滤特殊字符啊 一般来说 过滤了'就可以了 
      

  3.   

    用LIKE语句 
     
     
     
    ----------------------------------签----------名----------栏----------------------------------
      

  4.   

    string where="1=1";
    if(textbox1.Text!="")
    {
    where +="a='"+textbox1.Text+"'";
    }
    if(textbox2.Text!="")
    {
    where +="a='"+textbox2.Text+"'";
    }
    if(textbox3.Text!="")
    {
    where +="a='"+textbox3.Text+"'";
    }.........
    这样不行吗
      

  5.   

    string where="1=1";
    if(textbox1.Text!="")
    {
    where +="and a='"+textbox1.Text+"'";
    }
    if(textbox2.Text!="")
    {
    where +=" and  a='"+textbox2.Text+"'";
    }
    if(textbox3.Text!="")
    {
    where +=" and a='"+textbox3.Text+"'";
    }刚写错了额,。
      

  6.   

    请问 string where="1=1";和where +="and a='"+textbox1.Text+"'";  是什么意思?
      

  7.   

    SQL拼接呀 小喷油`参考`
      /// <summary>
            /// 根据日期,资源名称,资源描述查询资源信息
            /// </summary>
            /// <returns></returns>
            public DataSet FN_SerchByDateAndType(Guid FolderId, NRModel.File model, string createdate, string endate)
            {
                string strSql = "select * from t_File where 1 =1 and FolderId=@FolderId";
                string strWhere = "";
                if (!string.IsNullOrEmpty(model.FileNam))
                {
                    strWhere += " and FileNam like @FileNam";
                }
                //if (!string.IsNullOrEmpty(model.Decription)k)
                //{
                //    strWhere += " and Decription like @Decription";
                //}
                if (!string.IsNullOrEmpty(createdate) || !string.IsNullOrEmpty(endate))
                {
                    strWhere += " and CreateOn between @createdate and @endate order by ModefyOn desc";
                }
                strSql += strWhere;
                SqlParameter[] parameters = {
                                    new SqlParameter("@FolderId", SqlDbType.UniqueIdentifier),
                                    new SqlParameter("@FileNam", SqlDbType.NVarChar, 256),
                                    new SqlParameter("@createdate", SqlDbType.NVarChar),
                                    new SqlParameter("@endate", SqlDbType.NVarChar)
                            };
                parameters[0].Value = FolderId;
                parameters[1].Value = "%" + model.FileNam + "%";
                //parameters[1].Value = "%" + model.Decription + "%";
                parameters[2].Value = createdate;
                parameters[3].Value = endate;
                return DbHelperSQL.Query(strSql, parameters);
                //SqlParameter[] parameters = new SqlParameter[4];
                //parameters[0] = new SqlParameter("@FileNam", model.FileNam);
                //parameters[1] = new SqlParameter("@stardate", createdate);
                //parameters[2] = new SqlParameter("@enddate", endate);
                ////执行存储过程
                //return DbHelperSQL.RunProcedure("P_UserSerch", parameters, "t_File");
            }
      

  8.   

    这不就是一个组合查询麽。你再后台判断数据是否为空。不为空的话就拼接where条件就哦了
      

  9.   

    如果搜的是同一个字段,那么用in就可以了,还有把这8个文本框单独放到一个panel或者groupbox里,接着用控件方式遍历:
    string sql = "select * from table ";
    string where = "";
    foreach(Control ctl in panel1.Controls)
    {
        if(ctl is TextBox)
        {
            where += ','' + (ctl as TextBox).Text + ''';
        }
    }
    if(where != "")
    {
        sql = sql + "where a in (" + where.SubString(1) + ")";
    }
    //执行sql语句...略
    如果是不同字段,那就乖乖的if语句判断拼接吧。
      

  10.   

    where += ','' + (ctl as TextBox).Text + ''';
    引号写错了,改下:
    where += ",'" + (ctl as TextBox).Text + "'";
      

  11.   

    我如何运用到select语句中  是这样吗 SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["DbCompanyConnectionString"].ConnectionString);
            conn1.Open();
            string strwhere = "1=1";
            if (TextBox2.Text != "")
            {
                strwhere += "LawFJ='" + TextBox2.Text + "'";
            }
            if (TextBox3.Text != "")
            {
                strwhere += "LawZD='" + TextBox3.Text + "'";
            }
            if (TextBox4.Text != "")
            {
                strwhere += "Yyqxdw='" + TextBox4.Text + "'";
            }
            string sqlstr = "SELECT basic.*,[check].*,View_main.* FROM basic INNER JOIN [check] ON basic.cId = [check].CId INNER JOIN View_main ON basic.cId = View_main.CId where   + strWhere  ";
            SqlDataAdapter da1 = new SqlDataAdapter(sqlstr, conn1);
            DataSet ds1 = new DataSet();
            da1.Fill(ds1);
            this.GridView2.DataSource = ds1;
            this.GridView2.DataBind();
      

  12.   

    但是出错了  它说     在应使用条件的上下文(在 'strWhere' 附近)中指定了非布尔类型的表达式。 
      

  13.   

    你这样做,如果就只有一个以内文本框有值的,那是正常,但是2个以上有值,那么你的sql语句就有错了,where的每个条件缺少了用and连接