如何从mysql数据库添加项目到组合框 [英] how to add items to combo box from mysql database

查看:138
本文介绍了如何从mysql数据库添加项目到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨大家..我在这里有一个问题,在组合框中添加项目..例如我有一个数据库..我有一个名字,姓氏的样本字段..现在..这里的问题..我不知道如何获得一个例子的姓氏字段..这是我的代码..



  string  query =   select * from customer_tab; 
MySqlCommand cmd;

cmd = new MySqlCommand(query,conn);

MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable table = new DataTable( myTable );
da.Fill(table);

foreach (DataRow row in table.Rows)
{
for int i = 0 ; i < row.ItemArray.Length; i ++)
{
comboBox1.Items.Add(row.ItemArray [ 1 ]的ToString());

}
}

解决方案

我想我会将查询更改为只返回姓氏,如果这就是你需要的全部,那么将组合框的DataSource设置为查询结果而不是手动填充它。


  //  将查询更改为更具体 
// ==================================== =================================
string query = 从customer_tab中选择first_name;
MySqlCommand cmd;

cmd = new MySqlCommand(query,conn);

MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable table = new DataTable( myTable );
da.Fill(table);


// 使用此
// ========================== ===========================================
comboBox1.DataSource = da;
comboBox1.DisplayMember = first_name;
// ==================== =================================================



// 忘记所有这些
// ================= ================================================== ==
// foreach(table.Rows中的DataRow行)
// {
// for(int i = 0; i< row.ItemArray.Length; i ++)
// {
// comboBox1.Items.Add(row.ItemArray [1] .ToString());
//
// }
// }
// ====================================== ===============================


hi guys.. im having a problem here on adding items to a combo box.. for example i have a database.. i have which has a sample field of firstname,lastname.. now.. here's the problem.. i dont know how to get just the lastname field for an example.. here's my code..

string query = "select * from customer_tab";
MySqlCommand cmd;

cmd = new MySqlCommand(query, conn);

MySqlDataAdapter da = new MySqlDataAdapter(cmd);
DataTable table = new DataTable("myTable");
da.Fill(table);

foreach (DataRow row in table.Rows)
{
     for (int i = 0; i < row.ItemArray.Length; i++)
     {
          comboBox1.Items.Add(row.ItemArray[1].ToString());

     }
}

解决方案

I think I would change the query to return only the last name if that's all you need, then set the combo box's DataSource to the query results rather than manually filling it.


//Change query to be a little more specific
 //=====================================================================
 string query = "select first_name from customer_tab";
 MySqlCommand cmd;
 
 cmd = new MySqlCommand(query, conn);
 
 MySqlDataAdapter da = new MySqlDataAdapter(cmd);
 DataTable table = new DataTable("myTable");
 da.Fill(table);
 
 
 //Use this
 //=====================================================================
 comboBox1.DataSource = da;
 comboBox1.DisplayMember = "first_name";
 //=====================================================================
 
 
 
 //Forget all of this
 //=====================================================================
 //foreach (DataRow row in table.Rows)
 //{
 //     for (int i = 0; i < row.ItemArray.Length; i++)
 //     {
 //          comboBox1.Items.Add(row.ItemArray[1].ToString());
 //
 //     }
 //}
 //=====================================================================


这篇关于如何从mysql数据库添加项目到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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