[英]How to retrieve the ID from the table in the database and insert it to the SMTP client?
我正在開發一個Web應用程序,應將測驗自動發送給用戶。 現在,我正在編寫一個簡單的控制台應用程序,它將讀取數據庫中的Quiz表,其模式如下: QuizID,標題,描述,IsSent
IsSent是具有“位”數據類型的平面,用於指定是否發送消息的狀態。 我現在正在嘗試從數據庫中檢索QuizID和IsSent,並執行以下邏輯:如果未發送測驗,則將其發送給其他人不做任何事情
我正在編寫少量的代碼,現在我正努力地檢索QuizID和IsSent標志並將上述邏輯應用於這兩者。 那么該怎么做呢?
我的后台代碼:
protected void Page_Load(object sender, EventArgs e)
{
SmtpClient sc = new SmtpClient("MAIL ADDRESS");
MailMessage msg = null;
try
{
msg = new MailMessage("yyyyyyy@gmail.com",
"xxxxxxxxxx@gmail.com", "Message from PSSP System",
"This email sent by the system");
msg.IsBodyHtml = true;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (msg != null)
{
msg.Dispose();
}
}
//For requesting the Quiz_ID and check the IsSent flag for whether sending it or not
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
string cmdText = "SELECT QuizID, IsSent FROM dbo.QUIZ";
using (SqlConnection conn = new SqlConnection(connString))
{
conn.Open();
// Open DB connection.
using (SqlCommand cmd = new SqlCommand(cmdText, conn))
{
SqlDataReader reader = cmd.ExecuteReader();
While (reader != null){
if (reader.Read())
if (reader["IsSent"].Equals(0))
}
}
}
}
你可以做這樣的事情
long quizId;
int isSent;
While (reader != null){
if (reader.Read())
if (reader["IsSent"].Equals(0)){
quizId = Convert.ToInt64(reader["quizId"]);
isSent = Convert.ToInt32(reader["isSent"]);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.