2015-09-08 81 views
0

我有以下代碼循環通過數據庫表併發送電子郵件到存儲在每一行中的地址。如何修改它以在讀取它們的同時更新附加列?我想用當前日期和時間的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(); 
    } 

} 
+0

你試過了什麼? – vittore

+0

爲什麼不用[** Table Adapter **](https://msdn.microsoft.com/en-us/library/ms233819.aspx)代替? –

回答

0

您可以使用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("[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>"); 

    // 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(); 
    } 
+0

謝謝。我在某些命令中遇到了一些問題(例如需要將executeNonQuery更改爲ExecuteNonQuery)。但是,我現在得到一個錯誤,指出:「已經有一個與此命令關聯的打開的DataReader,必須先關閉它。」 UpdateCommand.ExecuteNonQuery();線? – user2335705

0

請勿使用DataReader。使用DataAdapter填充一個DataSet,然後遍歷它並更新DateSent列。循環後,更新DataSet。

0

我設法使用下面的代碼在第一個內部打開第二個連接。這可能是一種極其低效的做事方式,所以我會研究DataAdapter和DataSet,但至少現在它可以工作。

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 where Sent = 0"; 
     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.IsBodyHtml = true; 
     myMessage.Subject = "Test Message for " + (objReader.GetValue(1)) + " " + (objReader.GetValue(2)); 
     myMessage.Body = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> <title>Untitled Document</title> <style type='text/css'> body p { font-family: Verdana, Geneva, sans-serif; } body p { font-size: small; } </style> </head> <body> <p>Hello " + (objReader.GetValue(1)) + "</p> <p>This is a test email which will be sent to your email address: " + (objReader.GetValue(3)) + " when the system is live.</p> <p>Thanks.</p> </body> </html>"; 
     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>"); 

     // 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. 
     string strConn2 = "my connection string"; 
     SqlConnection objConnection2 = new SqlConnection(strConn2); 
     SqlCommand UpdateCommand = new SqlCommand("UPDATE EmailTable SET Sent = 1, DateSent = @dtSent WHERE id = @thisId", objConnection2); 
     objConnection2.Open(); 
     UpdateCommand.Parameters.AddWithValue("@dtSent", DateTime.Now); 
     UpdateCommand.Parameters.AddWithValue("@thisId", objReader.GetValue(0)); 
     UpdateCommand.ExecuteNonQuery(); 
     objConnection2.Close(); 
     } 
     objReader.Close(); 
     objConnection.Close(); 
    } 

} 
相關問題