如何从数据集中获取一个字符串中的单列多个记录? [英] How to get single column multiple records in one string from dataset?

查看:62
本文介绍了如何从数据集中获取一个字符串中的单列多个记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在此数据集中有emailid列表:

  public   string  get_emailid( string  circle)
{
string str2 = 从user_中选择emailid,其中circle =' + circle + '和role = 3;
SqlCommand cmd = new SqlCommand(str2,connection);
SqlDataAdapter da = new SqlDataAdapter(cmd );
DataSet ds = new DataSet();
da.Fill(ds);
string str = ds.Tables [0] .Rows [0] [
emailid ]。ToString();
return str;
}



返回以下电子邮件:

gandhi@gmail.com

chauhan@yahoo.com

kohli2@gmail.com



所以,当我使用SqlDataAd在aspx页面上调用此函数时apter da并存储在一个名为email的字符串中,我希望在另一个函数中使用此字符串电子邮件3次:

  string  email = da.get_emailid(Circle); 





我如何在一个字符串电子邮件中获得这3封电子邮件?



我尝试了什么:



i不想使用ArrayList list = new ArrayList();,我希望它使用for循环。我不知道......

解决方案

试试这个



 public string get_emailid(字符串圈,字符串拆分器)
{

string str2 =select userid from user_ where circle = @cirlce and role = 3;
SqlCommand cmd = new SqlCommand(str2,connection);
cmd.Parameters.AddWithValue(@ cirlce,circle);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
List< string> lst = new List< string>();
if(ds!= null&& ds.Tables.Count> 0&& ds.Tables [0] .Rows.Count> 0)
foreach(ds中的DataRow行) .Tables [0] .Rows)
lst.Add(row [emailid]。ToString());


string str = string.Join(splitter,lst);
return str;
}





 string email = da.get_emailid(circle,, ); //你应该使用任何拆分器来加入电子邮件







格式化sql查询字符串是易受攻击 SQL注入 [ ^ ]攻击

始终使用参数化查询以防止SQL Server中的SQL注入攻击 [ ^ ]


I have emailid list in this dataset:

public string get_emailid(string circle)
{
string str2 = "select emailid from user_ where circle='" + circle + "' and role=3;
SqlCommand cmd = new SqlCommand(str2, connection);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
string str = ds.Tables[0].Rows[0]["emailid"].ToString();
return str;
}


that returns below emails:
gandhi@gmail.com
chauhan@yahoo.com
kohli2@gmail.com

so, when i call this function on aspx page like this using SqlDataAdapter da and store in the one string named email, and i want use this string email 3 times in another function:

string email= da.get_emailid(Circle);



how can i get this 3 emailid in one string email?

What I have tried:

i dont want to use ArrayList list = new ArrayList();, i want it using for loop. I have no idea...

解决方案

try this

public string get_emailid(string circle, string splitter)
  {

      string str2 = "select emailid from user_ where circle= @cirlce and role=3 ";
      SqlCommand cmd = new SqlCommand(str2, connection);
      cmd.Parameters.AddWithValue("@cirlce", circle);
      SqlDataAdapter da = new SqlDataAdapter(cmd);
      DataSet ds = new DataSet();
      da.Fill(ds);
      List<string> lst = new List<string>();
      if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
          foreach (DataRow row in ds.Tables[0].Rows)
              lst.Add(row["emailid"].ToString());


      string str = string.Join(splitter, lst);
      return str;
  }



string email = da.get_emailid(circle,","); // you shall use any splitter to join the emails 




Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]


这篇关于如何从数据集中获取一个字符串中的单列多个记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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