当我循环阅读时,如何更新数据库中的行?

时间:2015-09-08 16:53:31

标签: c# sql asp.net

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

如果这是基本的初学者话,我还是新手,所以道歉。

感谢。

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("senderaddress@mydomain.com", "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();
    }

}

3 个答案:

答案 0 :(得分:2)

您可以使用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("senderaddress@mydomain.com", "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();
  }

答案 1 :(得分:0)

不要使用DataReader。使用DataAdapter填充DataSet,然后遍历该DataSet并更新DateSent列。循环后,更新DataSet。

答案 2 :(得分:0)

我现在使用下面的代码设法让它工作,在第一个代码中打开第二个连接。这可能是一种非常低效的处理方式,所以我会研究DataAdapters和DataSet,但至少它现在可以工作。

 .middle {
    margin: auto;
    width: 300px;
    background-color: #DEF0FF;
    margin-top: 64px;
}