0

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

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

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

预期结果: 由“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);
4

3 回答 3

0

使用执行阅读器。它会帮助你。
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 );
于 2013-04-15T09:34:57.687 回答
0

执行返回多条记录的查询时,您应该使用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());
于 2013-04-15T09:39:02.757 回答
0
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);
......................
于 2013-04-15T09:45:39.663 回答