我有以下代碼循環通過數據庫表併發送電子郵件到存儲在每一行中的地址。如何修改它以在讀取它們的同時更新附加列?我想用當前日期和時間的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("[email protected]", "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();
}
}
你試過了什麼? – vittore
爲什麼不用[** Table Adapter **](https://msdn.microsoft.com/en-us/library/ms233819.aspx)代替? –