[英]move listbox item to another listbox?
我想使用每個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
}
}
}
你不能在這里使用foreach
,因為你改變了循環中的listBox1.Items
,使用while循環並檢查listBox1.Items.Count() >0
並且在循環內你可以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);
}
}
注意: 您的代碼對於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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.