使用checkedListBox中的多个Checked Item创建查询

时间:2014-02-06 20:38:22

标签: c# mysql

我的代码在检查列表框中的1个已检查项目上工作正常,用户当前只能选择1项,但我希望用户能够选择多个复选框并循环,所以每个“from”+ SwitchClause +“。创建一个新查询并插入日期。

有一个我需要在底部实现的例子。

由于每个节点都是一个新的数据库,这是我能找到的最简单的方法。

感谢您的帮助。

public String GetQueriesForDay(DateTime Querydate)
{
    string SwitchName = "";
    foreach (DataRowView row in checkedListBox1.CheckedItems)
    {
        SwitchName = String.Format("{0}{1}{2}", SwitchName, (SwitchName == String.Empty) ? "" : "", row["name"]);
    }
    string SwitchClause = "";
    foreach (DataRowView row in checkedListBox1.CheckedItems)
    {
        SwitchClause = String.Format("{0}{1}node{2}", SwitchClause, (SwitchClause == String.Empty) ? "" : "", row["nodeid"]);
    }

    string Query;

    Query = String.Format("select '" + SwitchName + " - " + SwitchClause + "' as Switch_Name ,clngp_digits as Extn, cldp_talk_time, cost " +
                          "from "+ SwitchClause +".cr" + Querydate.ToString("yyyyMMdd") + " where record_type = 'D' or record_type = 'C' " +
                          "union all " +
                          "select '" + SwitchName + " - " + SwitchClause + "' as Switch_Name ,cldp_digits as Extn, cldp_talk_time, cost " +
                          "from " + SwitchClause + ".cr" + Querydate.ToString("yyyyMMdd") + " where record_type = 'D' or record_type = 'B'"
                          );

    return Query;
}
    private void button1_Click(object sender, EventArgs e)
    {

        CalculateDays();
        string SwitchClause = "";
        foreach (DataRowView row in checkedListBox1.CheckedItems)
        {
            SwitchClause = String.Format("{0}{1}node{2}", SwitchClause, (SwitchClause == String.Empty) ? "" : "", row["nodeid"]);
        }

        string SwitchName = "";
        foreach (DataRowView row in checkedListBox1.CheckedItems)
        {
            SwitchName = String.Format("{0}{1}{2}", SwitchName, (SwitchName == String.Empty) ? "" : "", row["name"]);
        }

        string queryStr = "";

        for (DateTime date = Startdate; date.Date <= Enddate.Date; date = date.AddDays(1))
        {
            queryStr = String.Format("{0}{1}{2}", queryStr, (date != Startdate) ? (date.AddDays(1) != Enddate) ? " UNION ALL " : " UNION ALL " : "", GetQueriesForDay(date));
        }


        string dataSource = IPtxt.Text;
        string tigerDatabase = "datasource =  " + IPtxt.Text + ";port=3306;DATABASE= " + SwitchClause + ";Uid=xxx;Pwd=xxx";


        MySqlConnection tigercon = new MySqlConnection(tigerDatabase);
        MySqlCommand callrec = new MySqlCommand("SELECT Switch_Name,Extn, COUNT(*) as Total_Calls, SEC_TO_TIME(SUM(cldp_talk_time/1000)) as Talk_Time, SUM(cost/100000) as 'Total_Cost' from (" + queryStr + ") t group by Extn order by Extn ASC ;", tigercon);

        try
        {
            MySqlDataAdapter ttrkvw = new MySqlDataAdapter();
            ttrkvw.SelectCommand = callrec;
            System.Data.DataTable dbdataset = new System.Data.DataTable();
            ttrkvw.Fill(dbdataset);
            BindingSource bSource = new BindingSource();

            bSource.DataSource = dbdataset;
            dataGridView1.DataSource = bSource;
            ttrkvw.Update(dbdataset);

            MySqlDataAdapter dap = new MySqlDataAdapter(callrec);
            System.Data.DataTable tblItems = new System.Data.DataTable();
            dap.Fill(tblItems);

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

所以下面的例子就是我想要实现的目标。这样,对于每个循环,它会为每天和每个选定的已检查项创建2个查询。节点。是已检查项目的值。此外,如果它有助于单词NodeX是数据库名称,表格格式为CRYYYYMMDD。

//Currently the next 3 lines is what i get with the current code and node1 selected in my checkbox.     
    select clngp_digits as Extn, cldp_talk_time, cost from node1.cr20140130 where record_type = 'D' or record_type = 'C'
    union all
    select cldp_digits as Extn, cldp_talk_time, cost from node1.cr20140130 where record_type = 'D' or record_type = 'B'
// If I have 2 checkbox selected then I would like the same as above with the other checkbox value I would then like it scaleable so if 10 check boxes are selected it will loop 10 times.
    select clngp_digits as Extn, cldp_talk_time, cost from node2.cr20140130 where record_type = 'D' or record_type = 'C'
    union all
    select cldp_digits as Extn, cldp_talk_time, cost from node2.cr20140130 where record_type = 'D' or record_type = 'B'

1 个答案:

答案 0 :(得分:0)

您可以使用

where record_type in ('D', 'I', ...)

这只会让您的代码变得更小。我建议创建一个视图,将您拥有的表分组为一个,这样查询就会轻松得多。

create view demo
as
select clngp_digits as Extn, cldp_talk_time, cost, record_type from node1.cr20140130
union all
select cldp_digits as Extn, cldp_talk_time, cost, record_type from node1.cr20140130
union all
select clngp_digits as Extn, cldp_talk_time, cost, record_type from node2.cr20140130
union all
select cldp_digits as Extn, cldp_talk_time, cost, record_type from node2.cr20140130

就这样查询

select *
from   demo
where  record_type in ('D', 'I', ...)
相关问题