我有以下代码,它循环遍历数据库表并将电子邮件发送到每行中存储的地址。如何修改它以在读取它们的同时更新附加列?我想用当前日期和时间的 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/