如何将两个列值添加到列表中
如何将两列值添加到列表中,这两列位于不同的表中。电子邮件位于 tbl_user 表中,组名位于 tbl_group 表中。我想使用自动完成文本框的电子邮件和组名称的前缀文本。现在,通过查询,我可以将电子邮件 ID 添加到列表中,当我输入 arr 时,会显示以 arr 开头的电子邮件 ID,如何为组名和电子邮件 ID 执行此操作
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
MySqlCommand command = new MySqlCommand();
string textforlist = "SELECT tbl_user.Email from tbl_user WHERE tbl_user.OrganisationID = '1' AND UserActive='Yes'AND tbl_User.Email like '" + prefixText + "%'";
DataSet ds = new DataSet();
MySqlParameter[] param = new MySqlParameter[1];
param[0] = new MySqlParameter("@OrganisationID", MySqlDbType.Int32);
param[0].Value = count;
command.Parameters.AddWithValue("@OrganisationID", count);
using (DataServer server = new DataServer())
{
ds = server.ExecuteQuery(CommandType.Text, textforlist, param);
}
string textforlist1 = "SELECT tbl_group.GroupName from tbl_group WHERE tbl_group.OrganisationID = '1' AND GroupActive='Yes'AND tbl_group.GroupName like '" + prefixText + "%'";
DataSet ds1 = new DataSet();
using (DataServer server = new DataServer())
{
ds1 = server.ExecuteQuery(CommandType.Text, textforlist1, param);
}
List<string> cityList = new List<string>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cityList.Add(ds.Tables[0].Rows[i][0].ToString());
cityList.Add(ds1.Tables[0].Rows[i][0].ToString());
}
return cityList.ToArray();
}
How can I add two column value to a list, the two column are in different table. email is in tbl_user table and groupname is in tbl_group table. And I want to use the prefix text for both the email and group name for the auto complete text box. Now with the query I am able to add the email id to the list and when I type arr then email id starting with arr are displayed how can I do it for both groupname and emailID
public static string[] GetCompletionList(string prefixText, int count, string contextKey)
{
MySqlCommand command = new MySqlCommand();
string textforlist = "SELECT tbl_user.Email from tbl_user WHERE tbl_user.OrganisationID = '1' AND UserActive='Yes'AND tbl_User.Email like '" + prefixText + "%'";
DataSet ds = new DataSet();
MySqlParameter[] param = new MySqlParameter[1];
param[0] = new MySqlParameter("@OrganisationID", MySqlDbType.Int32);
param[0].Value = count;
command.Parameters.AddWithValue("@OrganisationID", count);
using (DataServer server = new DataServer())
{
ds = server.ExecuteQuery(CommandType.Text, textforlist, param);
}
string textforlist1 = "SELECT tbl_group.GroupName from tbl_group WHERE tbl_group.OrganisationID = '1' AND GroupActive='Yes'AND tbl_group.GroupName like '" + prefixText + "%'";
DataSet ds1 = new DataSet();
using (DataServer server = new DataServer())
{
ds1 = server.ExecuteQuery(CommandType.Text, textforlist1, param);
}
List<string> cityList = new List<string>();
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
cityList.Add(ds.Tables[0].Rows[i][0].ToString());
cityList.Add(ds1.Tables[0].Rows[i][0].ToString());
}
return cityList.ToArray();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需添加其他列值即可吗?也许我没有正确理解这个问题,但这应该很简单 - 你的表有两列,
Email
是第一列,GroupName
是第二列:你可以修改你的sql查询限制返回的组名,类似于当前对电子邮件所做的操作 - 不过,您确实应该为此使用 SQL 参数,否则您很容易受到 SQL 注入攻击。
编辑:
使用更新后的代码,您应该使用两个 for 循环
Just add the other column value as well? Maybe I don't understand the question correctly but this should be straightforward - your table has two columns,
Email
is the first andGroupName
the second:You can modify your sql query to restrict the returned group names similar to how you currently do it for Email - you really should use SQL parameters for this though, otherwise you are open for SQL injection attacks.
Edit:
With your updated code you should use two for loops
如果您的表格列选择增加,那么您将需要用自定义对象填充列表。要实现这一点,您必须首先创建一个自定义对象类。
In case your table column selection increase then you will be requiring List filled with Custom objects. To achieve you must create a custom object class first.