c#中如何用线程实现每天零点去执行一个更新数据库的方法.急求,求大神帮忙

c#中如何用线程实现每天零点去执行一个更新数据库的方法.急求,求大神帮忙,第1张

尝试下面的思路:每隔 一小时执行一次:

public delegate void UpdateText(string content)

    public partial class Form1 : Form

    {

        Thread t

        public Form1()

        {

            InitializeComponent()

        }

        /// <summary>

        /// 强制执行过期未完成的工作 

        /// 存储过程名:Proc_ForceExecute

        /// </summary>

        private string ForceExecuteTimeOut() 

        {

            string connString = GetXmlNodeText("ConnectionString")

            SqlConnection conn = new SqlConnection(connString)

            SqlCommand cmd = new SqlCommand("Proc_ForceExecute", conn)

            cmd.CommandType = CommandType.StoredProcedure

            try

            {

                conn.Open()

                cmd.ExecuteNonQuery()

                conn.Close()

                return "[" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "]进行检测"

            }

            catch (Exception ex)

            {

                conn.Close()

                return "出现错误:" + ex.Message

            }

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            t = new Thread(new ThreadStart(ThreadMethod))

            t.Start()

        }

        private void ThreadMethod() 

        {

            //每隔多少秒执行一次 配置文件单位:秒

            int ms = int.Parse(GetXmlNodeText("TimeSpan")) * 1000

            while (true)

            {

                string res = ForceExecuteTimeOut()

                UpdateText mitxt = new UpdateText(UpdateRichTextBox)

                if (this.IsHandleCreated) 

                {

                    this.Invoke(mitxt, res)

                }

                Thread.Sleep(ms)

            }

        }

        private void UpdateRichTextBox(string content) 

        {

            richTextBox1.Text = richTextBox1.Text + content + "\n"

        }

      }

Timer time = new Timer()//定义一个 Timer

//执行方法 里面需要一个类 可以定义一个伪类

//也可以 自己 定义一个类 继承TimerTask这个类

//里面必须实现 run方法

//run方法 里面就写你的 实做 方法

//schedule 方法有很多重载 有很多参数我用最简单的

//给你说明 第2个参数是 说 每隔一定的时间执行一次

//相关说明可以去jdk 帮助文档里面找

time.schedule(new TimerTask() {

public void run() {

int inSertValue{

try{

commection con1=con

Statement stmt=con1.createStatement()

str="(insert ……)"

……

stmt.close()

} catch()

} } //加到这里就好了!!最好把这样方法封装起来

}

}, 1000)

merge GroupStage a using

(select GroupStageId, sum(Adult+Children) over() as sumAC from LineSchedule where ScheduleDate<='2014/9/23 10:37:19') b on a.GroupStageId=b.GroupStageId

when matched then update set a.ApplyBit=b.sumAC


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/sjk/6760620.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存