如何将两个列值添加到列表中

发布于 2024-12-10 20:35:48 字数 1525 浏览 0 评论 0原文

如何将两列值添加到列表中,这两列位于不同的表中。电子邮件位于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橙味迷妹 2024-12-17 20:35:48

只需添加其他列值即可吗?也许我没有正确理解这个问题,但这应该很简单 - 你的表有两列,Email是第一列,GroupName是第二列:

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
   cityList.Add(ds.Tables[0].Rows[i][0].ToString());
   cityList.Add(ds.Tables[0].Rows[i][1].ToString());
}

你可以修改你的sql查询限制返回的组名,类似于当前对电子邮件所做的操作 - 不过,您确实应该为此使用 SQL 参数,否则您很容易受到 SQL 注入攻击。

编辑:

使用更新后的代码,您应该使用两个 for 循环

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    cityList.Add(ds.Tables[0].Rows[i][0].ToString());
}

for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
    cityList.Add(ds1.Tables[0].Rows[i][0].ToString());
}

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 and GroupName the second:

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
   cityList.Add(ds.Tables[0].Rows[i][0].ToString());
   cityList.Add(ds.Tables[0].Rows[i][1].ToString());
}

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

for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
    cityList.Add(ds.Tables[0].Rows[i][0].ToString());
}

for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
    cityList.Add(ds1.Tables[0].Rows[i][0].ToString());
}
请帮我爱他 2024-12-17 20:35:48

如果您的表格列选择增加,那么您将需要用自定义对象填充列表。要实现这一点,您必须首先创建一个自定义对象类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文