如何在特定日期和特定时间区分以下代码 [英] How To exceute following code at specific day and specific time

查看:95
本文介绍了如何在特定日期和特定时间区分以下代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,



我创建一个应用程序,应用程序一个文件在特定时间发送给用户并且特定日将sql数据导出到excle,发送功能的电子邮件工作正常,但问题是它无法执行特定的时间和特定的一天我粘贴在这里我的代码请帮助我

Hello friends,

I am create one application that application one file send to user at specific time and specific day export sql data into excle ,sending email that functions working fine but problems is that it cannot execute particular time and particular day I am paste here my code please help me out

if ((DateTime.Now.DayOfWeek== DayOfWeek.Saturday) &&(DateTime.Now.Hour==19) &&(DateTime.Now.Minute==15))
            {
                exportsqldatatoexcle();

                emailattachment();
              
                
            }

          
            
        }

        public void exportsqldatatoexcle()
        {
            string con1 = "Data Source=ADMIN\\SQLEXPRESS;Initial Catalog=PhysioCure; Integrated Security=true";
            SqlConnection connection = new SqlConnection(con1);
            connection.Open();
            sda = new SqlDataAdapter("select PationName,RegistrationDate,ContactNo,Age,Sex,Chief_Complain,Investigation_Result,PastHistoryAny,Physical_Examination,Medications,Prognosis,Electro_Therapy,Neuro_Rehabilitation,Ortho_Rehabilitation,Cardio_Pulmonery_Rehabilitation,Sports_Rehabilitation from Physio_cureTable order by RegistrationDate desc", con1);
            dt = new DataTable();
            sda.Fill(dt);

            DataColumnCollection dccollection = dt.Columns;

            Microsoft.Office.Interop.Excel.ApplicationClass excelapp = new Microsoft.Office.Interop.Excel.ApplicationClass();

            excelapp.Application.Workbooks.Add(Type.Missing);


            for (int i = 1; i < dt.Rows.Count + 1; i++)
            {

                for (int j = 1; j < dt.Columns.Count + 1; j++)
                {
                    if (i == 1)
                    {
                        excelapp.Cells[i, j] = dccollection[j - 1].ToString();

                    }
                    else
                    {
                        excelapp.Cells[i, j] = dt.Rows[i - 1][j - 1].ToString();

                    }

                }

            }

            excelapp.ActiveWorkbook.SaveCopyAs("E:\\Winform n console\\PhysioCure\\PhysioCure\\PhysioCure\\Patient_Details.xls");
            excelapp.ActiveWorkbook.Saved = true;
            excelapp.Quit();
            MessageBox.Show("Detail file successfully create!");

        }

        private void emailattachment()
        {
            //try
            //{
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("My email id");
                mail.To.Add("My frnd email id");
                mail.Subject = "Sending you Patient_Details File - 1";
                mail.Body = "Please Check Mail With Attachment";

                System.Net.Mail.Attachment attachment;
                attachment = new Attachment("E:\\Winform n console\\PhysioCure\\PhysioCure\\PhysioCure\\Patient_Details.xls");
                mail.Attachments.Add(attachment);

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("My email id", "Password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);

                MessageBox.Show("Details File send on your mail please check your mail");



已添加代码块[/ Edit]


Code block added[/Edit]

推荐答案

您需要创建一个Windows服务,它在系统启动时开始运行。此服务将继续检查日期和时间,并将执行您的功能。您可以在codeproject中搜索以了解Windows服务的工作原理。



但有一条建议。请在帖子中避免使用短信术语。它使您的问题难以理解。花点时间解释一下你的问题。通过这种方式,您将得到更好的响应。
You need to create a windows service which starts running when the system boots. This service will keep checking for the day and time and will execute your function. You can search in codeproject to understand how a windows service works.

One piece of advice though. Kindly avoid SMS lingo in your postings. It makes a hard understanding of your question. Take time to explain your problem. In that way you will get better response.


您有两个选择:使用服务器任务调度程序,或使用SQL服务器作业代理





http://technet.microsoft.com/en-us/ library / cc721871.aspx [ ^ ]



http:// msdn .microsoft.com / zh-CN / library / ms191439.aspx [ ^ ]
You have two options: using server task scheduler , or using SQL server job agent


http://technet.microsoft.com/en-us/library/cc721871.aspx[^]

http://msdn.microsoft.com/en-us/library/ms191439.aspx[^]


这篇关于如何在特定日期和特定时间区分以下代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆