c# - 当我循环读取数据库中的行时,如何更新它们?

标签 c# sql asp.net

我有以下代码,它循环遍历数据库表并将电子邮件发送到每行中存储的地址。如何修改它以在读取它们的同时更新附加列?我想用当前日期和时间的 DateSent 值更新每一行。该表包含五列 - ID、名字、姓氏、电子邮件、发送日期 - 这是我想要更新特定电子邮件发送日期和时间的最后一列。

我对此还很陌生,所以如果这是基本的初学者内容,我深表歉意。

谢谢。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Net.Mail;

public partial class displayRecords : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
      string strConn = "my connection string;";
      string strSQL = "select * from EmailTable";
      SqlConnection objConnection = new SqlConnection(strConn);
      SqlCommand objCommand = new SqlCommand(strSQL, objConnection);
      objConnection.Open();
      SqlDataReader objReader = objCommand.ExecuteReader();
      while (objReader.Read())
      {

        MailMessage myMessage = new MailMessage();
        myMessage.Subject = "Test Message for " + (objReader.GetValue(1)) + " " + (objReader.GetValue(2));
        myMessage.Body = "This email would be sent to: " + (objReader.GetValue(3));
        myMessage.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2c5f494248495e4d48485e495f5f6c41554843414d4542024f4341" rel="noreferrer noopener nofollow">[email protected]</a>", "Sender Name");
        myMessage.To.Add(new MailAddress((objReader.GetString(3)), (objReader.GetString(2))));

        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.Send(myMessage);

        Response.Write("Email sent to: " + (objReader.GetValue(3)) + "<br>");
      }
      objReader.Close();
      objConnection.Close();
    }

}

最佳答案

您可以使用 UPDATE 语句根据 ID 更新行。

  while (objReader.Read())
  {

    MailMessage myMessage = new MailMessage();
    myMessage.Subject = "Test Message for " + (objReader.GetValue(1)) + " " + (objReader.GetValue(2));
    myMessage.Body = "This email would be sent to: " + (objReader.GetValue(3));
    myMessage.From = new MailAddress("<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2d5e484349485f4c49495f485e5e6d40544942404c4443034e4240" rel="noreferrer noopener nofollow">[email protected]</a>", "Sender Name");
    myMessage.To.Add(new MailAddress((objReader.GetString(3)), (objReader.GetString(2))));

    SmtpClient mySmtpClient = new SmtpClient();
    mySmtpClient.Send(myMessage);

    Response.Write("Email sent to: " + (objReader.GetValue(3)) + "<br>");

    // Update the table, assuming ID is the first column in the table.
    // This is for demonstration only and it is not the most efficient way
    // of doing this because a new command is created each time. 
    // The correct way would be to move the command and parameters creation
    // outside the loop and just update the parameter values inside the loop.
    SqlCommand UpdateCommand = new SqlCommand("UPDATE EmailTable SET DateSent = @dtSent WHERE id = @thisId", objConnection);
    updateCommand.Parameters.AddWithValue( "@dtSent", DateTime.Now);
    updateCommand.Parameters.AddWithValue( "@thisId", objReader.GetValue(0));
    updateCommand.ExecuteNonQuery();
  }

关于c# - 当我循环读取数据库中的行时,如何更新它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32463230/

相关文章:

c# - Panel.Dock Fill 忽略其他 Panel.Dock 设置

c# - 如何使用 Moq 为异步函数抛出异常

php - Facebook 如何在不到一秒的时间内检查电子邮件是否存在?

mysql - 使用 WHERE AND 的多参数化 SQL 语法无效

javascript - 在登录 asp.net MVC 应用程序期间检测 javascript...这段代码看起来正常吗?

c# - SelectedValue 在 DropDownList 中回发时失败

c# - 为 daterange 切片数据表,总结计数并创建自定义表?

c# - 在 ASP.NET Web 应用程序中将异步/等待与任务一起使用时,UI 线程被阻塞

sql - 根据条件逻辑更新表列值

c# - 按名称查找标签并从后面的代码中设置标签文本