简体   繁体   English

如何从数据库表中检索ID并将其插入SMTP客户端?

[英]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.

相关问题 在插入语句后从 SQL 数据库中检索自动增量 ID - Retrieve Auto Increment ID from SQL Database after Insert Statement 从xml文件检索数据并插入数据库表 - Retrieve data from xml file and insert into database table 从SQL Server数据库表中检索最后一个ID - Retrieve the last id from the SQL Server database table 如何从表1获取用户名的ID并将其插入表2 - How to get id of username from table 1 and insert that id to table 2 如何从一个表中检索数据并插入另一个表中? - How to retrieve a data from a table and insert into another table? 如何使用正确的字符集从sqlite数据库中插入和检索数据? - How to insert and retrieve data from sqlite database with correct character set? 从数据库检索数据并将这些值插入数据库C#中的另一个表 - retrieve data from database and insert those values to another table in database c# 如何从Smtp客户端发送邮件? - How to send mail from Smtp client? 如何从mdb的表中检索最后一个主要ID? - How to retrieve last primary Id from mdb's table? 如何仅使用 asp.net C# 在具有无限记录(例如地址)和此客户端的唯一 ID 的数据库表中插入一个人? - How can I Insert a person in a database table with unlimited records ( addresses for example ) and a unique ID for this client only using asp.net C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM