我有一个名为“lnkupdate_click”的按钮,基本上它在您单击时所做的就是访问一个名为“tbl_security”的表,然后它会使用随机的 4 位数字更新字段“pass”,其中字段“patrol”= 1 或 true .
一个例子如下所示;
Code Pass Patrol
TES 1234 1
ASD 4321 1
MOR 6789 1
SAI 0959 1
我坚持的一点是:我希望它从这里做的就是通过电子邮件从数据库中发送更新的信息。因此,基本上是为我生成一封包含上表中所有信息的电子邮件。但是,我的“foreach”循环下方不断出现一条红线,并带有此错误消息。
“foreach 语句不能对 'int' 类型的变量进行操作,因为 'int' 不包含 'GetEnumerator' 的公共定义”
我的代码如下:
protected void lnkUpdate_Click(object sender, EventArgs e)
{
{
string queryUpdateAllFields;
string queryGetAllUpdatedField;
StringBuilder sb = new StringBuilder();
queryGetAllUpdatedField = @"update tbl_Security set
Pass = round(rand(CAST(CAST(NEWID() AS VARBINARY(4)) AS SMALLINT))* 9000,0) + 1000
WHERE Patrol = 1";
queryGetAllUpdatedField = @"SELECT Code, Pass, Patrol
FROM tbl_Security
WHERE Patrol = 1";
var random = new Random();
//queries to update and retrieve
SqlHelper.ExecuteSqlNonQuery(queryUpdateAllFields);
var results = SqlHelper.ExecuteSqlNonQuery(queryGetAllUpdatedField);
//loop over the results and append to StringBuilder instance.
sb.Append("Below are all the fields that have been updated: <br /><br />");
foreach(var r in results)
{
sb.AppendLine("<hr />");
sb.AppendLine("Code: " + r.Code + " Pass: " + r.Pass + " Patrol: " + r.Patrol); //for Patrol
}
//Email showing all updated pass's with codes
MailAddress to = new MailAddress("");
MailAddress from = new MailAddress("");
MailMessage message = new MailMessage(from, to);
message.Subject = "Subject Line Here";
message.Body = sb.ToString(); //attach StringBuilder - This will be built up of all the rows updated in the DataBase
SmtpClient mailClient = new SmtpClient();
mailClient.Port = 25;
//finally send message here
mailClient.Send(msg);
}
}