【发布时间】:2018-05-02 06:20:29
【问题描述】:
我有一个名为 "Events" 的表,其中有分别名为 'EquipID' 和 'EmailSent' 的列。
插入数据时,'EmailSent' 的默认值为 "no"。现在我必须运行一个查询来遍历每一行是否已根据值发送电子邮件。如果查询读取 'no',那么我必须执行 SMTP 函数,根据相应的 'EquipID' 向它发送邮件,我必须在其中获取它的单元格值。如果查询改为 'yes',则可以跳过一行。
现在我不知道如何调用表并查询迭代以获取单元格的值,仅当 'EmailSent' 的值为 'no' >.
到目前为止,我做过类似的事情。
con.Open();
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MVCConnectionString"].ConnectionString))
{
//replace this with your query
using (var command = new SqlCommand("SELECT EventID, EquipID, EmailSent FROM Events", con))
{
con.Open();
using (var reader = command.ExecuteReader())
{
if (reader.HasRows)
{
while (reader.Read())
{
if (reader["EmailSent"].ToString() == "no")
{
string IDIDID = reader["EquipID"].ToString();
//add your function to send email
string sqlView = "SELECT * FROM [NewEquipment] INNER JOIN [User] ON [NewEquipment].[UserID] = [User].[UserID] WHERE EquipID = '" + IDIDID + "'";
using (SqlCommand yes = new SqlCommand(sqlView, con))
{
SqlDataReader read = yes.ExecuteReader();
if (read.Read())
{
string UserEmail = read["UserEmailAdd"].ToString();
string UserFullName = read["UserFullName"].ToString();
string EquipIDID = read["EquipID"].ToString();
string ModelNo = read["ModelNo"].ToString();
string ModelDesc = read["ModelDesc"].ToString();
string CalType = read["CalType"].ToString();
string CalDate = read["EquipCalDueDate"].ToString();
DateTime caldate = DateTime.Parse(CalDate);
string DateDate = caldate.ToString("MM-dd-yyyy");
MailMessage mail = new MailMessage();
mail.To.Add(UserEmail);
mail.From = new MailAddress("keysight@keysight.com");
mail.Subject = "Reminder on Equipment's Cal Due Date";
mail.IsBodyHtml = true;
string Body = "Greetings " + UserFullName + "<br/><br/>This email is to remind you that you have " + "<b>10 days </b>" + "left before you can send the equipment for calibration. Below are the details of the respective equipment: " +
"<br/><br/>Equipment ID: " + EquipIDID + "<br/>Model No.: " + ModelNo + "<br/>Model Description: " + ModelDesc + "<br/>Cal Type: " + CalType + "<br/>Equipment Status: " + "<b>CRITICAL</b>" +
"<br/>Equipment Cal Due Date: " + DateDate + "<br/><br/>Thank you." + "<br/><br/>Regards," + "<br/>Keysight Technologies";
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.cos.is.keysight.com";
smtp.Port = 25;
smtp.Send(mail);
}
read.Close();
con.Close();
}
}
}
}
}
}
}
con.Close();
【问题讨论】:
-
学习如何查询数据库是一个很好的目标。不幸的是,StackOverflow 不是一个教程网站。好消息是那里有“数不胜数”的网站可以提供您需要的指导。
-
您没有提供任何有关您检索数据的技术的信息...例如,您是否使用实体框架?
-
是的,我正在使用实体框架模型来调用其他页面 Hooman 中的数据库。
-
var results = db.Events.Where(x => x.EmailSent == "no").Select(x => x.EquipID);将为您提供所有EquipID的集合,其中EmailSent == "no"
标签: sql asp.net-mvc datatable iterator local-database