如何连接数据库中的2个数据并将其显示在列表框中 [英] How to concatenate 2 data in database and show it in a listbox
问题描述
您好!我有一个具有firstname和lastname字段的数据库。我想检索两个字段并将其绑定到列表框中。
我已经设法将列表框中的数据与单个字段绑定。使用这样的代码:
Hello! I have a database that has a firstname and lastname field. I want to retrieve the two fields and bind it into a listbox.
I''ve manage to bind the datas in a listbox with a single field only. With code like this:
void LoadGrid()
{
MySqlConnection sqlcon = new MySqlConnection(strConnString);
sqlcon.Open();
sqlcmd = new MySqlCommand("SELECT lastname from userdbase", sqlcon);
da = new MySqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ListBox1.DataSource = dt;
ListBox1.DataTextField = "lastname";
ListBox1.DataBind();
}
}
但我不知道如何插入另一个字段并将其连接到此第一。有帮助吗?谢谢。
示例:
firstname ||姓氏
----------------------
Kristine ||拉莫斯
玛丽|| Mariano
列表框中的输出将是:
Kristine Ramos
Mary Mariano
But I don''t know how to insert another field and concatenate it with this first one. Any help? Thanks.
Example:
firstname || lastname
----------------------
Kristine || Ramos
Mary || Mariano
Output in listbox will be:
Kristine Ramos
Mary Mariano
推荐答案
hi buddy
这就是你想要的......
只按照设置属性您的要求..
hi buddy
this will that you want....
just set properties as per requirement of yours..
try
{
con.Open();
string str = "select CONCAT(1stname, ' (' ,lastname,')') AS mixname from userdbase where pfno='" + sd.epfno + "'";
da = new MySqlDataAdapter(str, con);
ds = new DataSet();
da.TableMappings.Add("table", "userdbase");
da.Fill(ds, "userdbase");
this.listBox1.DataSource = this.ds;
this.listBox1.DisplayMember = "userdbase.mixname ";
this.listBox1.ValueMember = "userdbase.mixname";
//listBox1.MultiColumn = true;
con.Close();
da.Dispose();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex.Message);
}
乐意提供帮助
happy to help
我想出了这段代码。它工作正常。
I came up with this code. And it works fine.
MySqlConnection sqlcon = new MySqlConnection(strConnString);
sqlcon.Open();
string sqlcmd = "select CONCAT(firstname, '' ('' ,lastname,'')'') AS mixname from userdbase";
da = new MySqlDataAdapter(sqlcmd,sqlcon);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ListBox1.DataSource = dt;
ListBox1.DataTextField = "mixname";
ListBox1.DataBind();
}
试试这个>
Try This->
void LoadGrid()
{
MySqlConnection sqlcon = new MySqlConnection(strConnString);
sqlcon.Open();
sqlcmd = new MySqlCommand("SELECT (ISNULL(firstname, '') + '' + ISNULL(lastname, '')) FROM userdbase", sqlcon);
da = new MySqlDataAdapter(sqlcmd);
dt.Clear();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
ListBox1.DataSource = dt;
ListBox1.DataTextField = "lastname";
ListBox1.DataBind();
}
}
这篇关于如何连接数据库中的2个数据并将其显示在列表框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!