包含SQL的电子邮件

本文关键字:电子邮件 SQL 包含 | 更新日期: 2023-09-27 17:49:15

我有一个名为"lnkupdate_click"的按钮,当你点击时,它所做的基本上就是访问一个名为"tbl_security"的表,从那里它将更新字段"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);
      }
  }

包含SQL的电子邮件

ExecuteSqlNonQuery返回一个int不是一个集合,我认为你是假设。

try:这样改变循环:for(int i= 0; i < results; i++)