用C#如何每隔一段时间链接数据库读取数据怎么做啊

解决方案 »

  1.   

    启一个线程,每一个时间段读取一次。可以看看MSDN中的Timer
      

  2.   

    使用System.Timers.Timer
    用法和winform的timer很像,设置有效性Enabled和时间间隔Interval,使用Start和Stop方法控制其开始与停止计时,开始计时后Timer每隔一定时间触发Elapsed事件,向该事件中添加想要调用的方法即可。
      

  3.   

    Timer的唯一事件   在事件里面定义链接库的方法
    或者用Thread线程,用Sleep()来定义多长时间链接库一次
      

  4.   

    设置好时间,使用TIMER周期扫描就可以了。注意几点:1,扫描周期不要跳过设置访问数据库的时间;2,数据库连接之后,注意关闭连接,以释放资源。
      

  5.   

    使用System.Timers.Timer 
    用法和winform的timer很像,设置有效性Enabled和时间间隔Interval,使用Start和Stop方法控制其开始与停止计时,开始计时后Timer每隔一定时间触发Elapsed事件,向该事件中添加想要调用的方法即可。 
    System.Timers.Timer 
     比较准确  public int wrong = 0;
            System.Timers.Timer time = new System.Timers.Timer();
            
            private void begin_Click(object sender, EventArgs e)
            {
                if (action.Text == "启动监测")
                {
                    action.Text = "停止监测";
                    label2.Text = "已启动";                if (time.Interval.ToString() == "100") // The default value of interval is 100s.
                    {
                        time.Elapsed += new ElapsedEventHandler(TimeEvent);
                        time.Interval = 1000;
                    }
                    time.Enabled = true;
                }
                else
                {
                    action.Text = "启动监测";
                    label2.Text = "已停止";                time.Enabled = false;
                }        }        private static void TimeEvent(object source, ElapsedEventArgs e)
            {
                int tsec = e.SignalTime.Second;
                int isec = 10;
                if (tsec == isec) //it will be activated at 10s of every minutes.
                {
                    if (!Check("http://www.test.com"))
                    {
                        string smtp_server="192.168.8.1";
                        int port = 25;
                        string mail_from = "[email protected]";
                        string sender="test";
                        string mail_to = "[email protected]";
                        string receiver="adminer";
                        string subject = "The site is run out exception on " + DateTime.Now.ToString("yyyyMMddhhmmss");
                        string body = "The site can not open on " + DateTime.Now.ToString() + ",please check it !";
                        try
                        {
                            SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);
                        }
                        catch(Exception ex)
                        {
                            MessageBox.Show(ex.Message);
                        }
                    }
                }
            }        private static bool Check(string urlStr)
            {
                HttpWebRequest myWebRequest = null;
                try
                {
                    myWebRequest = (HttpWebRequest)WebRequest.Create(urlStr);
                    HttpWebResponse res = (HttpWebResponse)myWebRequest.GetResponse();
                    if (res.StatusCode == HttpStatusCode.OK)
                    {
                        res.Close();
                        return true;
                    }
                    else
                    {
                        res.Close();
                        return false;
                    }
                }
                catch (Exception)
                {
                    return false;
                }
            }        public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)
            {
                MailAddress from = new MailAddress(mail_from, sender);
                MailAddress to = new MailAddress(mail_to, receiver);
                MailMessage message = new MailMessage(from, to);
                message.BodyEncoding = Encoding.UTF8;
                message.IsBodyHtml = true;
                message.Subject = subject;
                message.Body = body;            SmtpClient client = new SmtpClient(smtp_server, port);
                //SmtpClient client = new SmtpClient(smtp_server);            // Add credentials if the SMTP server requires them. 
                client.Credentials = CredentialCache.DefaultNetworkCredentials;
                client.Send(message);
            }
    本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/pipi_sn/archive/2009/07/21/4366376.aspx
      

  6.   

    还可以:Thread th=new Thread(MyMethod);
    th.IsBackground=true;
    th.Start();
    private void MyMethod()
    {
       while(true)
       {
          //查询数据库,进行你的处理
          Thread.Sleep(间隔时间秒*1000);
       }
    }