[英]How to retrieve the ID from the table in the database and insert it to the SMTP client?
I am developing a web application that should send quizzes to the users automatically. 我正在开发一个Web应用程序,应将测验自动发送给用户。 Now, I am working in writing simple console application that will read the Quiz table in the database which its schema as following: QuizID, Title, Description, IsSent
现在,我正在编写一个简单的控制台应用程序,它将读取数据库中的Quiz表,其模式如下: QuizID,标题,描述,IsSent
IsSent is a flat with Bit data type which specified the status of message sent or not. IsSent是具有“位”数据类型的平面,用于指定是否发送消息的状态。 I am trying now to retrieve the QuizID and IsSent from the database and doing the following logic: If the quiz was not sent, sends it Else don't do anything
我现在正在尝试从数据库中检索QuizID和IsSent,并执行以下逻辑:如果未发送测验,则将其发送给其他人不做任何事情
I am writing the small amount of code and I struggled now with retrieving the QuizID and IsSent flag and apply the above logic on both of them. 我正在编写少量的代码,现在我正努力地检索QuizID和IsSent标志并将上述逻辑应用于这两者。 So HOW TO DO THAT?
那么该怎么做呢?
My code-behind: 我的后台代码:
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))
}
}
}
}
You can do something like this 你可以做这样的事情
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.