【问题标题】:Get column data from sql database and store in label/string从 sql 数据库中获取列数据并存储在标签/字符串中
【发布时间】:2013-04-15 18:14:55
【问题描述】:

我打算只从 SQL 数据库中检索列数据并将其存储在字符串或标签中作为持有者。

案例: 从数据库 "tblUser" WHERE apple = 1 中选择所有电子邮件地址并存储在 "emaillist" 字符串持有者中,然后附加 mail.to.add(emaillist);

注意: ExecuteScalar 只允许我检索数据库的第一行。因此,只有第一行电子邮件地址能够检索。 (例如,user@user.com)

预期结果: emaillist 由“user@user.com,testing.gmail.com,admin@admin.com”等组成。

我的代码:

     String status = "1";
    SqlConnection conn100 = new SqlConnection("Server=localhost;Integrated                     Security=true;database=WebsiteDatabase");
    conn100.Open();
    SqlCommand cmd100 = conn100.CreateCommand();
    cmd100.CommandText = "select emailaddress from tblUser where apple= '" + status + "'";
        string selected100 = cmd100.ExecuteScalar().ToString();

        String emailcontent = "";
        emailcontent = "" + selected1 + "- Highlight: " + selected2;
        String emaillist = "" + selected100;
        //Create a new MailMessage in order to send email
        MailMessage mail = new MailMessage();

        //The recipient of this email
        mail.To.Add("recipient@gmail.com" + emaillist);

        //The sender of this email
        mail.From = new MailAddress("sender@gmail.com");

        //The subject of this email
        mail.Subject = "Latest Product";

        //The email's content
        string body = "New Product added! " + emailcontent;
        mail.Body = body;
        mail.IsBodyHtml = true;

        //Create new smtpClient and use smtp services
        SmtpClient smtp = new SmtpClient();

        //SMTP host (Gmail)
        smtp.Host = "smtp.gmail.com";

        //SMTP port
        smtp.Port = 587;
        smtp.UseDefaultCredentials = false;

        //Sender Email and password
        smtp.Credentials = new System.Net.NetworkCredential("sender@gmail.com", "password");

        //Enable SSL in order to have secure transmission
        smtp.EnableSsl = true;

        //Send the email
        smtp.Send(mail);

【问题讨论】:

  • 当执行返回多条记录的查询时,您应该使用 SqlDataReader 然后循环返回的行。
  • 您的意图在这里不是很清楚(至少对我而言),您是否返回了多个电子邮件地址 - 您可能想group by 他们以防止重复?此外,ExecuteScalar 应该只用于返回单个值 - 如果您选择一行,您只会得到它的第一列。

标签: c# asp.net


【解决方案1】:

当执行返回多条记录的查询时,您应该使用SqlDataReader,然后遍历返回的行

....
conn100.Open();
SqlCommand cmd100 = conn100.CreateCommand();
cmd100.CommandText = "select emailaddress from tblUser where apple= @status";
cmd100.Parameters.AddWithValue("@status", status);
SqlDataReader dr = cmd100.ExecuteReader();
StringBuilder sb = new StringBuilder();
while(dr.Read())
{
   sb.AppendFormat("{0},", dr[0].ToString());
}

我还更改了您的代码以删除 sql text 命令中的字符串连接,我还使用了 StringBuilder 来构建您的目标列表,因为当您有许多字符串要连接在一起时它会更有效。

....
// Now the StringBuilder contains all of your To Address separated by a comma as required 
// Clip away the last comma
sb.Length = sb.Length - 1;
mail.To.Add(sb.ToString());

【讨论】:

    【解决方案2】:
    String status = "1";
    SqlConnection conn100 = new SqlConnection("Server=localhost;Integrated                     Security=true;database=WebsiteDatabase");
    conn100.Open();
    SqlCommand cmd100 = conn100.CreateCommand();
    cmd100.CommandText = "select emailaddress from tblUser where apple= '" + status + "'";
        SqlDataReader dr= cmd100.ExecuteReader();
    while(dr.Read())
    {
    string selected100= dr("emailaddress").ToString();
    }
    
        String emailcontent = "";
        emailcontent = "" + selected1 + "- Highlight: " + selected2;
        String emaillist = "" + selected100;
        //Create a new MailMessage in order to send email
        MailMessage mail = new MailMessage();
    
        //The recipient of this email
        mail.To.Add("recipient@gmail.com" + emaillist);
    ......................
    

    【讨论】:

      【解决方案3】:

      使用执行阅读器。它会帮助你。
      MSDN链接http://msdn.microsoft.com/en-us/library/9kcbe65k.aspx

      string str="";
      SqlCommand command = new SqlCommand(queryString, connection);
      SqlDataReader reader = command.ExecuteReader();
      while (reader.Read())
      {
         str+=dr[0].ToString()+",";
      }
      

      并删除末尾的最后一个逗号。

      或者你可以利用 List 如下

        var userEmail = new List<string>();
      
        while (lobjDtReader.Read())
        {
             userEmail .Add(lobjDtReader[0].ToString());
        }
      
        var Emails = string.Join(",", userEmail );
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-15
        • 2014-02-18
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多