我在使用 SQL 语句迭代的数据库中有 3 个表。它搜索需要续签合同的应用程序。我使用 SQL 日期数学来检查是否需要通知经理续签合同。如果今天的日期 = notificationDate 字段中的日期,则控制台应用程序应向列为该应用程序的分析师/经理的人发送电子邮件。到目前为止,这是我的代码:
namespace ContractApp
{
class Program
{
//initializes strings for storing information from the table
static string aretheyManager;
static string listedanalystEmail;
static string listedmanagerEmail;
static void Main(string[] args)
{
int warningWindow = 10;
try
{
//connects to the AppInfo_dev table in the database
SqlConnection conn = new SqlConnection("server=10.52.2.169\\sqlcluster,1206;uid=TannerAppsWriter;pwd=TannerAppsWriter;database=AppInfo_dev;");
conn.Open();
//sets up a sequal command called selectedValues
SqlCommand selectValues = conn.CreateCommand();
//Pulls information from three tables in the database (AppInfo_dev, SoftwareApp, IT_Personnel)
//Takes the AppID from the Contracts list and compares it to AppID in the Apps list and displays matches
//Then it finds employee information related to the Primary Analyst that is listed for that application
//Adds a field called "notificationDate" that is filled by subtracting the "warningWindow" and "TerminationWindow" from the RenewalDate
//Finds contracts listed that have a "notificationDate" that is the same as the current date
//Takes the eMail fields and appends "@tanner.org" to the end of the text in the field
selectValues.CommandText = "My SQL statement goes here...it works so I didn't bother posting it since it is really long"
//Reads values in specified columns in the database
using (SqlDataReader dataReader = selectValues.ExecuteReader())
{
if (dataReader.HasRows)
{
while (dataReader.Read())
{
//Converts the values in the tables to strings
aretheyManager = Convert.ToString(dataReader["isManager"]);
listedanalystEmail = Convert.ToString(dataReader["analystEmail"]);
listedmanagerEmail = Convert.ToString(dataReader["managerEmail"]);
}
}
}
}
//If there is an error, catch it
catch (SqlException e)
{
Console.WriteLine(e.Message);
}
}
private void sendEmailNotification()
{
//Create an email to send notifying of contract termination
MailMessage message = new MailMessage();
//Check to see if the listed analyst is a manager
//If they are, send the email to them
//If they are not, send they email to their manager.
if (aretheyManager == "True")
{
message.To.Add(listedanalystEmail);
}
else
{
message.To.Add(listedmanagerEmail);
}
message.Subject = "This contract requires your attention!";
message.From = new MailAddress("no response email address goes here");
message.Body = "There is an application contract that is in need of renewal.";
message.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient("client info goes here");
client.Send(message);
}
}
}
SQL 语句按预期工作。它遍历表中的行并使用 notificationDate = 当前日期提取合同。我在使用数据读取器时遇到问题。它遍历由 SQL 语句提取的合约,但仅将它读取的最后一个值存储到字符串中。我需要它来存储它提取的所有值,以便在需要通知多个人时向每个人发送一封电子邮件。