返回首页

如何检索数据:基于多重选择|专家您好,

我乌代Satardekar,

我开发网站在asp.net.I已使用的MySQL作为后端。

我view_event.aspx页,我显示所有活动的基础上选择。

为此,有4个基本条件。 3 DropDownList的(状态,类别,国家)

日期间(2日期控件)。

现在按一下按钮,我必须从数据库中显示的结果。

我们先前4标准atlist的1,是,necessary.user可以选择一个以上的标准也。

我曾尝试下面的代码的国家和类别selection.But的不正常工作。

MySqlConnection connect = null;

        try

        {

            int country = Convert.ToInt32(lstCountry.SelectedIndex);

            int category = Convert.ToInt32(lstCategory.SelectedIndex);

            string status = lstStatus.SelectedItem.Text;

            string startdate = txtStartDate.Text.Trim();

            string enddate = txtEndDate.Text.Trim();

 

            string connectStr = ConfigurationManager.ConnectionStrings["ExpoCrmConnectionString"].ToString();

            connect = new MySqlConnection(connectStr);

            connect.Open();

 

            string members = "SELECT crm_event.event_id,crm_event.event_name,crm_event.event_status,crm_countries.country_name,crm_event.venue,crm_category.category,crm_event.start_date,crm_event.end_date FROM crm_event LEFT JOIN crm_category ON crm_event.cat_id=crm_category.cat_id LEFT JOIN crm_countries ON crm_event.country=crm_countries.country_id " +

            "WHERE" +

            "((country IS NULL) OR (country=?country)) AND "+

            "((category IS NULL) OR (crm_event.cat_id=?cat_id))";

            

            MySqlCommand command = new MySqlCommand(members, connect);

            command.Parameters.AddWithValue("?country", country);

            command.Parameters.AddWithValue("?cat_id", category);

            

            

            MySqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);

            DataTable dt = new DataTable();

            dt.Load(reader);

            listvwEventResult.DataSource = dt;

            listvwEventResult.DataBind();

            connect.Close();

        }

        catch (Exception ex)

        {

 

        }

        finally

        {

            connect.Close();

        }


问题是关于如何写select查询,其中包括所有4个标准...
有时,用户可以选择的国家,有时国家和日期,有时国家和状态和日期。

如何在一个查询来管理这些条件吗?
请帮助我。

感谢在前进。

回答

评论会员:AmarSinghRawat 时间:2012/02/06
{C5的}
通过此行"lstCategory.SelectedIndex"你正在选定的指数。指1,2,3等

如果绑定DropDownList的值字段中的CategoryID,然后你应该使用
{5233}
或者如果您的CategoryID绑定DropDownList的文本字段,那么你应该使用
{C7-}
评论会员:supriyachaladi 时间:2012/02/06
Devide分为2部分在这里你的代码是我的代码的一个例子
{C8的}