如何从数据库表中检索ID并将其插入SMTP客户端?
我正在开发一个Web应用程序,该应用程序应自动将测验发送给用户.现在,我正在编写一个简单的控制台应用程序,它将读取数据库中的Quiz表,其模式如下:
*** QuizID,标题,描述,已发送***
IsSent是具有Bit数据类型的平面,用于指定是否发送消息的状态.我现在正在尝试从数据库中检索QuizID和IsSent,并执行以下逻辑:
如果未发送测验,请发送
其他
什么都不要做
我正在编写少量代码,而现在我却在努力获取QuizID和IsSent标志并将它们应用于以上逻辑.那怎么办呢?
我的后台代码:
I am developing a web application that should send quizzes to the users automatically. 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***
IsSent is a flat with Bit data type which specified the status of message sent or not. 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
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. 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))
}
}
}
}
string connString = "Data Source=localhost\\sqlexpress;Initial Catalog=psspdb;Integrated Security=True";
string cmdText = "SELECT QuizID, IsSent FROM dbo.QUIZ where isnull(IsSent,0)=0";
// then agove query filters only not sent records
// this is the condition : "where isnull(IsSent,0)=0"
string sLinkId = "";
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())
{
string sQid = reader["QuizID"].ToString();
sLinkId += "<a href="http://websitename.com/quizepage.aspx?quizeid=" + sQid + "">Quize ID :" + sQid + "</a>";
}
}
}
}
SmtpClient sc = new SmtpClient("MAIL ADDRESS");
MailMessage msg = null;
try
{
msg.To.Add("mailid1@gmial.com");
msg.To.Add("mailid2@gmial.com");
msg.To.Add("mailid3@gmial.com");
msg.From = new MailAddress("yourmailid@gmail.com");
msg.Subject = "Message from PSSP System";
msg.Body = "This Mail Send by System :" + sLinkId;
msg.IsBodyHtml = true;
sc.Send(msg);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (msg != null)
{
msg.Dispose();
}
}
试试这个...
Try This...