日期:2014-05-17  浏览次数:20521 次

GridView循环显示问题?
一个GridView,一个DropDownList,一个Button ,
DropDownList为(0,最近7天),(1,最近15天),(2,最近30天),(3,查询所有)
当button点击时,根据DropDownList的SelectedIndex值查询。
该怎么循环查询好些呢。

C# code
    protected void btn_cha_Click(object sender, EventArgs e)
    {
        if (ddl7.SelectedIndex == 0)
        {
            GridView1.DataSource = null;
            string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<7 and user_name='" + Session["userName"] + "'";
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            GridView1.DataSource = sdr;
            GridView1.DataBind();
            con.Close();
            sdr.Close();
        }
        if (ddl7.SelectedIndex == 1)
        {
            GridView1.DataSource = null;
            string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<15 and user_name='" + Session["userName"] + "'";
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader sdr = cmd.ExecuteReader();

            GridView1.DataSource = sdr;
            GridView1.DataBind();
            con.Close();
            sdr.Close();
        }
        if (ddl7.SelectedIndex == 2)
        {
            GridView1.DataSource = null;
            string sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<30 and user_name='" + Session["userName"] + "'";
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            GridView1.DataSource = sdr;
            GridView1.DataBind();
            con.Close();
            sdr.Close();
        }
        if (ddl7.SelectedIndex == 3)
        {
            GridView1.DataSource = null;
            string sql = "select * from [jinhuo] where user_name='" + Session["userName"] + "'";
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            GridView1.DataSource = sdr;
            GridView1.DataBind();
            con.Close();
            sdr.Close();
        }
    }


------解决方案--------------------
别的不说,如下
C# code

private void ExecSql(string sql)
{
            GridView1.DataSource = null;
            SqlConnection con = new SqlConnection(conString);
            con.Open();
            SqlCommand cmd = new SqlCommand(sql, con);
            SqlDataReader sdr = cmd.ExecuteReader();
            GridView1.DataSource = sdr;
            GridView1.DataBind();
            con.Close();
            sdr.Close();
}

    protected void btn_cha_Click(object sender, EventArgs e)
    {
            string sql = "";
        if (ddl7.SelectedIndex == 0)
        {
            sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<7 and user_name='" + Session["userName"] + "'";
        }
        if (ddl7.SelectedIndex == 1)
        {
           sql = "select * from [jinhuo] where datediff(day,j_date,'" + DateTime.Now.ToShortDateString() + "')<15 and user_name='" + Session["userName"] + "'";
        }
        if (ddl7.SelectedI