如何使用列表框从表中选择多个项目 [英] how to select multiple items from table with listbox

查看:72
本文介绍了如何使用列表框从表中选择多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位大家好,亲爱的,我有疑问,如果你能帮助我:)



i希望通过我从listbox中选择的多个过滤器从表中检索数据/>
i想在我的应用程序中使用此查询

 选择 * 来自产品其中 firstname = '  sam'  firstname = '  remhel' 



和我选择的名称被加载到列表框,

i试图以某种方式但是不正确

这是我的代码c#

 List< string> selecteditmes =  new  List< string>(); 
foreach int i in listBox1.SelectedIndices)
{
selecteditmes.Add(listBox1.Items [i] .ToString());

string smdd = select * from productvw其中Firstname =' + i + '或Firstname = + i + ;

SqlDataAdapter sqld = new SqlDataAdapter(smdd,cn);
cn.Open();

DataTable dt = new DataTable();
sqld.Fill(dt);
dataGridView1.DataSource = dt;
// dataGridView1.Refresh();
cn.Close();
}
}

私有 void Form1_Load( object sender,EventArgs e)
{
SqlCommand cmd = new SqlCommand(< span class =code-string> 从productvw中选择名字,cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
string smd =( string )dr [ 名字]的ToString();
listBox1.Items.Add(smd);
}
cn.Close();
}

解决方案

你可以这样试试:



  int  selectedCounter = listBox1.SelectedItems.Count; 

// 接下来的几行用于预构建连接字符串WITH PARAMETERS
字符串 sqlQuery = 选择*来自productvw,其中;
for int i = 0 ; i < selectedCounter; i ++)
{
if (i != 0
{
sqlQuery + = ;
}
sqlQuery + = Firstname = @Fn + i.toString ();
}

使用(SqlConnection connection = new SqlConnection(connectionString ))
{
使用(SqlCommand command = new SqlCommand(sqlQuery,Connection ))
{
for int i = 0 ; i < selectedCounter; i ++)
{
command.Parameters.AddWithValue( @ Fn + i.toString(); listBox1.SelectedItem [i] .Text);
Connection.Open();
SqlDataReader reader = command.ExecuteReader();
while (Reader.Read())
{
// 在这里做读者的事情
}
}
}
}


hello everyone ,dear i have question if you could please help me :)

i want to retrieve data from table with multiple filter that i chose from listbox
i want to use this query in my application

select * from product where firstname='sam' or firstname ='reachel'


and the name i select is loaded to listbox ,
i tried somehow but wasn't correct
and this is my code of c#

List<string> selecteditmes = new List<string>();
            foreach (int i in listBox1.SelectedIndices)
            {
                selecteditmes.Add(listBox1.Items[i].ToString());

 string smdd = "select * from productvw  where Firstname ='"+ i +"'or Firstname="+i+"";

                SqlDataAdapter sqld = new SqlDataAdapter(smdd, cn);
                cn.Open();

                DataTable dt = new DataTable();
                sqld.Fill(dt);
                dataGridView1.DataSource = dt;
                //dataGridView1.Refresh();
                cn.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            SqlCommand cmd = new SqlCommand("select Firstname from productvw ", cn);
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                string smd = (string)dr["Firstname"].ToString();
                listBox1.Items.Add(smd);
            }
            cn.Close();
        }

解决方案

You could try it like this:

int selectedCounter = listBox1.SelectedItems.Count;

//the next few rows are for pre-constructing the Connection string WITH PARAMETERS
String sqlQuery = "select * from productvw where ";
for (int i = 0; i < selectedCounter; i++)
{
    if(i != 0)
    {
        sqlQuery += " or ";
    }
    sqlQuery += "Firstname = @Fn" + i.toString();
}

using (SqlConnection connection = new SqlConnection(connectionString))
{
    using (SqlCommand command = new SqlCommand(sqlQuery, Connection))
    {
        for (int i = 0; i < selectedCounter; i++)
        {
            command.Parameters.AddWithValue("@Fn"+i.toString();listBox1.SelectedItem[i].Text);
            Connection.Open();
            SqlDataReader reader = command.ExecuteReader();
            while (Reader.Read())
            {
                //do stuff with the reader here
            }
        }
    }
}


这篇关于如何使用列表框从表中选择多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆