首页 文章

将列表框项目移动到另一个列表框?

提问于
浏览
0

我想使用每个listbox1项来运行这两个查询,如果两个结果都不相同,那么将该项移动到名为listbox2的另一个列表框(如果它们是相同的,则从listbox1中删除该项) .

foreach (string Items in listBox1.Items)
{
    using (OracleCommand crtCommand = new OracleCommand("select count(*) from(( select * from all_ind_columns where  index_name= '" + Items + "'  and table_owner='" + txtSrcUserID.Text.ToUpper() + "'))", conn1))
    using (OracleCommand ctCommand = new OracleCommand("select count(*) from(( select * from all_ind_columns where  index_name= '" + Items + "'  and table_owner='" + txtDesUserID.Text.ToUpper() + "'))", conn1))
    {
        string result1 = crtCommand.ExecuteScalar().ToString();
        string result2 = ctCommand.ExecuteScalar().ToString();

        if (result1 != result2)
        {
            //move that item to listbox2
        }
        else if(result1 == result2)
        {
            // remove that item from listbox1
        }
    }
}

1 回答

  • 2

    你不能在这里使用 foreach ,因为你在循环中更改 listBox1.Items ,使用while循环并检查 listBox1.Items.Count() >0 并在循环内你可以拍摄第一个项目并将其移动到第二个项目或删除 .

    while (ListBox1.Items.Count>0)
    {
        var item =  ListBox1.Items[0].ToString();
    
        using (OracleCommand crtCommand = new OracleCommand("select count(*) from(( select * from all_ind_columns where  index_name= '" + item + "'  and table_owner='" + txtSrcUserID.Text.ToUpper() + "'))", conn1))
        using (OracleCommand ctCommand = new OracleCommand("select count(*) from(( select * from all_ind_columns where  index_name= '" + item + "'  and table_owner='" + txtDesUserID.Text.ToUpper() + "'))", conn1))
        {
            string result1 = crtCommand.ExecuteScalar().ToString();
            string result2 = ctCommand.ExecuteScalar().ToString();
            if (result1 != result2)
            {
                ListBox2.Items.Add(item);
            }
            ListBox1.Items.RemoveAt(0);
        }   
    
    }
    

    Note: 您的代码对于sql注入攻击是开放的,使用参数而不是内联参数 .

    while (ListBox1.Items.Count>0)
    {
        var item =  ListBox1.Items[0].ToString();
    
        using (OracleConnection con = new OracleConnection(connectionString))
        using (OracleCommand cmd = con.CreateCommand())
        {
            con.Open();
            cmd.CommandText = "select count(*) from(( select * from all_ind_columns where  index_name= :item  and table_owner=:table_owner))";
            cmd.Parameters.Add(item);
            cmd.Parameters.Add(txtSrcUserID.Text.ToUpper());
    
            string result1 = cmd.ExecuteScalar().ToString();
            cmd.Parameters.Clear();
            cmd.Parameters.Add(item);
            cmd.Parameters.Add(txtDesUserID.Text.ToUpper());
            string result2 = cmd.ExecuteScalar().ToString();
    
            if (result1 != result2)
            {
                ListBox2.Items.Add(item);
            }
            ListBox1.Items.RemoveAt(0);
        }
    
    }
    

相关问题