我打算只从SQLdatabase中检索列数据,并将其作为持有者存储在字符串或标签中。
CASE: 从数据库“tblUser”WHERE apple = 1中选择所有电子邮件地址并存储在“emaillist”字符串中,然后附上mail.to.add(emaillist);
注意: ExecuteScalar只允许我检索我的数据库的第一行。因此,只有第一行电子邮件地址能够检索。 (例如,用户@ 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);
答案 0 :(得分:0)
使用executereader。它会对你有所帮助。
string str="";
SqlCommand command = new SqlCommand(queryString, connection);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
str+=dr[0].ToString()+",";
}
并删除最后的最后一个逗号。
或者您可以从列表中获益
var userEmail = new List<string>();
while (lobjDtReader.Read())
{
userEmail .Add(lobjDtReader[0].ToString());
}
var Emails = string.Join(",", userEmail );
答案 1 :(得分: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());
答案 2 :(得分: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);
......................