0

リストボックス1の各アイテムを使用して両方のクエリを実行し、両方の結果が同じでない場合は、そのアイテムをリストボックス2と呼ばれる別のリストボックスに移動し、リストボックス1からそのアイテムを削除します。

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
        }
    }
}
4

1 に答える 1

2

foreachループ内を変更するため、ここでは使用できませんlistBox1.Items。 while ループを使用してチェックし、listBox1.Items.Count() >0ループ内で最初の項目を選択して 2 番目の項目に移動するか、削除することができます。

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);
    }   

}

注: コードは 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);
    }

}
于 2013-07-26T04:09:40.007 に答える