1、关于多项查询,我设了四个查询,
用户查询可以用任何一个条件,点击一个button即可,怎么样写代码?麻烦写出具体的步骤以及整体的代码示例,这是我的部分代码,实在不会,用的是if嵌套,有24种可能,请大家改正。用的是vs2005        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["information"].ToString());
        conn.Open();
        if (TextBox1.Text == null)
        {
            if (DropDownList1.Text == null)
            {
                if (DropDownList2.Text == null)
                {
                    if (TextBox2.Text == null && TextBox3.Text == null)
                    {
                        Response.Write("<script>alert('请输入数据!')</script>");
                    }
                    if (TextBox2.Text != null && TextBox3.Text != null)
                    {
                        string str = "select 项目名 from preprojects where 招商截止时间>='" + TextBox2.Text + "'and  招商截止时间<='" + TextBox3.Text + "'";
                        
                        SqlDataAdapter da1 = new SqlDataAdapter(str, conn);
                        DataSet ds1 = new DataSet();
                        da.Fill(ds1);
                        GridView1.DataSource = ds1;
                        GridView1.DataBind();
                        
                    }
                    else
                    {
                        Response.Write("<script>alert('请输入正确日期!')</script>");
                    }
                }
                else
                {
                    if (TextBox2.Text == null && TextBox3.Text == null)
                    {
                        string str = "select 项目名 from preprojects where 项目类型='" + DropDownList2.Text + "'";
                        
                        SqlDataAdapter da2 = new SqlDataAdapter(str, conn);
                        DataSet ds2 = new DataSet();
                        da.Fill(ds2);
                        GridView1.DataSource = ds2;
                        GridView1.DataBind();
                        
                    }
                    if (TextBox2.Text != null && TextBox3.Text != null)
                    {
                        string str = "select 项目名 from preprojects where 项目类型='" + DropDownList2.Text + "' and 招商截止时间>='" + TextBox2.Text + "'and  招商截止时间<='" + TextBox3.Text + "'";
                       
                        SqlDataAdapter da3 = new SqlDataAdapter(str, conn);
                        DataSet ds3 = new DataSet();
                        da.Fill(ds3);
                        GridView1.DataSource = ds3;
                        GridView1.DataBind();
                        
                    }
2、向左转|向右转
怎样将gridview1选中的一行插入到数据库中的另一个表中,也是不会,方法特笨,代码如下:if (e.CommandName == "insert")
        {
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["information"].ToString());
            conn.Open();            string str = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[0].Text.ToString();
            string str1 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[1].Text.ToString();
            string str2 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[2].Text.ToString();
            string str3 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[3].Text.ToString();
            string str4 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[4].Text.ToString();
            string str5 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[5].Text.ToString();
            string str6 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[6].Text.ToString();
            string str7 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[7].Text.ToString();
            string str8 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[8].Text.ToString();
            string str9 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[9].Text.ToString();
            string str10 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[10].Text.ToString();
            string str11 = GridView1.Rows[int.Parse(e.CommandArgument)].Cells[11].Text.ToString();
            Int16 num = Convert.ToInt16(str);
            DateTime dt1 = Convert.ToDateTime(str9);
            SqlCommand cmd = new SqlCommand("insert into preprojects values (" + num + ",'" + str1 + "',,'" + str2 + "',,'" + str3 + "',,'" + str4 + "','" + str5 + "','" + str6 + "','" + str7 + "','" + str8 + "','" + dt1 + "','" + str10 + "','" + str11 + "')",conn);
            cmd.ExecuteNonQuery();
            Response.Write("<script>alert('通过')</script>");
            conn.Close();        } 最终结果:第一个button怎么点也不出结果,没有任何错误显示。第二个显示“与“int.Parse(string)”最匹配的重载方法具有一些无效参数”。初学者,所以很多不懂,不甚感激! ASP.NETGridViewdataset

解决方案 »

  1.   

    1.查询条件拼接
    string sql = "select * from table where 1=1";            if (!string.IsNullOrEmpty(TextBox1.Text))//筛选条件不为空
                    sql += string.Format(" and 条件1='{0}'", TextBox1.Text);//条件1为表中对应的查询列
                if (!string.IsNullOrEmpty(TextBox2.Text))
                    sql += string.Format(" and 条件2='{0}'", TextBox2.Text);//条件2为表中对应的查询列
    2.虽然显得笨,却是基本的,可以改进为参数化查询
      

  2.   

    strng sql="select * from user where 1=1 ";if(txtuserid.Text!="")
    {
       sql+=" and userid='"+txtuserid.Text+"'";
    }
    if(txtusername.Text!="")
    {
       sql+=" and username='"+txtusername.Text+"'";
    }执行sql  凑合看吧!自己改进一下
      

  3.   

    第一个button怎么点也不出结果,没有任何错误显示:你那不是有if条件吗,会不会不满足if,所以没有进行下一步操作,你调试下吧
      

  4.   

    第二个button类型转换问题,你调试下,改下类型不就可以了吗