Skip to main content
 首页 » 编程设计

c#之如何在 while 循环中将数据读取器的值传递给 C# 中的方法

2024年02月13日19softidea

我在数据库中有 3 个表,我正在使用 SQL 语句对其进行迭代。它搜索需要续签契约(Contract)的应用程序。我使用 SQL 日期数学来检查是否需要通知经理续签契约(Contract)。如果今天的日期 = 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 = 当前日期的契约(Contract)。我在使用数据读取器时遇到问题。它遍历 SQL 语句提取的契约,但只将它读取的最后一个值存储到字符串中。我需要它来存储它提取的所有值,以便在需要通知多人时向每个人发送一封电子邮件。

请您参考如下方法:

数据读取器不是为保存数据而设计的。它只是遍历数据。如果你想存储 “读取”的结果,将数据添加到 ArrayList 或其他一些数据结构中,以便您对数据执行进一步的工作。

 con.Open(); 
 
    ArrayList al = new ArrayList(); 
    SqlDataReader dr = cmd.ExecuteReader(); 
 
    while(dr.Read()) { 
        object[] values = new object[dr.FieldCount]; 
        dr.GetValues(values); 
        al.Add(values); 
    } 
 
    dr.Close(); 
    con.Close();