如何定时的去执行SQL语句? lz 同时用的服务方式。可以使用 windows的 “任务计划程序”功能, 在 我的电脑-》管理中。新建一个任务,指定成你的控制台的exe可以执行程序,里边操作有详细步骤,可以设置时间区间。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 Oracle有Job,你也可以给电脑创建任务计划... http://www.cnblogs.com/IPrograming/archive/2012/03/08/SQLServer_Timer.html定时任务 看他的圖好像是C#裡的webserver,所以你在那裡加這個控件 System.Timers.Timer timer = new System.Timers.Timer();我寫了個類型功能的小程式,我是用服務寫的,具體功能代號就不帖出來了using System;using System.Collections.Generic;using System.Threading;using System.ComponentModel;using System.Data;using System.Diagnostics;using System.ServiceProcess;using System.Text;using System.IO;namespace DBTableDataTransServer{ public partial class DBTableDataTransServer : ServiceBase { System.Timers.Timer timer = new System.Timers.Timer(); DataTable tbExecPlan = new DataTable(); int nTimes = 0; bool IsCanExec = true; bool IsExecOk = false; string strCurrentTime = ""; string strExecSeq = ""; string strSeqMark = ""; string strExecTime = ""; bool IsOver = true; public DBTableDataTransServer() { InitializeComponent(); } protected override void OnStart(string[] args) { // TODO: 在此处添加代码以执行開啟服务所需的操作。 //記錄服務開啟日誌 WebHp.WriteServiceLog("A"); //讀取配置文件 tbExecPlan = WebHp.LoadXml("B"); //設置每隔1分鍾發生一次事件 timer.Interval = 60000; timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed); timer.Enabled = true; timer.Start(); } protected override void OnStop() { // TODO: 在此处添加代码以执行停止服务所需的关闭操作。 //記錄服務開啟日誌 WebHp.WriteServiceLog("B"); } private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //當前時間 strCurrentTime = DateTime.Now.ToString("yyyyMMddHHmm"); strExecSeq = "";//本次要執行的項次 strSeqMark = ""; nTimes = nTimes + 1; //讀取計劃執行的日誌 //日誌格式,如0001項次 //按年:0001:2014 Begin at 201410101230 //按月: 0001:201410 Begin at 201410101230 //按天: 0001:20141001 Begin at 201410101230 try { if (IsCanExec && IsOver) { IsOver = false; string strText = WebHp.ReadPlanExecLog(); if (strText == null) { strText = ""; } else { if (strText == "Error") { IsCanExec = IsOver = true; return; } } for (int i = 0; i < tbExecPlan.Rows.Count; i++) { if (tbExecPlan.Rows[i]["IsOpen"].ToString() == "False") { continue; } strExecTime = tbExecPlan.Rows[i]["ExecTime"].ToString().Replace("-", "").Replace(":", "").Replace(":", "").Replace("-", ""); switch (tbExecPlan.Rows[i]["ExecType"].ToString().Substring(0,1)) { case "A"://按年執行 if (strExecTime.CompareTo(strCurrentTime.Substring(4, 8)) <= 0) { if (strText.IndexOf(tbExecPlan.Rows[i]["Seq"] + ":" + strCurrentTime.Substring(0, 4)) >= 0) { continue; } strExecSeq = tbExecPlan.Rows[i]["Seq"].ToString(); strSeqMark = strExecSeq +":"+ strCurrentTime.Substring(0, 4) + " "; } break; case "B"://按月執行 if (strExecTime.CompareTo(strCurrentTime.Substring(6, 6)) <= 0) { if (strText.IndexOf(tbExecPlan.Rows[i]["Seq"] + ":" + strCurrentTime.Substring(0, 6)) >= 0) { continue; } strExecSeq = tbExecPlan.Rows[i]["Seq"].ToString(); strSeqMark = strExecSeq + ":" + strCurrentTime.Substring(0, 6) + " "; } break; case "C"://按天執行 if (strExecTime.CompareTo(strCurrentTime.Substring(8, 4)) <= 0) { if (strText.IndexOf(tbExecPlan.Rows[i]["Seq"] + ":" + strCurrentTime.Substring(0, 8)) >= 0) { continue; } strExecSeq = tbExecPlan.Rows[i]["Seq"].ToString(); strSeqMark = strExecSeq + ":" + strCurrentTime.Substring(0, 8) + " "; } break; case "D"://循環執行 if (nTimes / 2 >= Convert.ToInt32(tbExecPlan.Rows[i]["ExecTime"].ToString())) { nTimes = 0; strExecSeq = tbExecPlan.Rows[i]["Seq"].ToString(); strSeqMark = strExecSeq + ":" + strCurrentTime.Substring(0, 10) + " "; } break; } if (strExecSeq != "") { break; } } if (strExecSeq != "") { //寫入執行開始日誌 strText = strSeqMark + " 開始執行時間:" + strCurrentTime + "->" + strExecSeq + ":End"; string strRe=WebHp.WritePlanExecLog(strText, "A"); //開始執行此項次 IsCanExec = false; strRe = WebHp.TransData(strExecSeq, "B"); if (strRe.Substring(strRe.Length - 2, 2) == "OK") { IsExecOk = true; } IsCanExec = true; } IsOver = true; } } catch (System.Exception ex) { IsOver = true; IsCanExec = true; } finally { if (strExecSeq != "") { if (IsExecOk) { WebHp.WritePlanExecLog(strExecSeq + ":End", "B"); } else { WebHp.WritePlanExecLog(strExecSeq + ":End", "C"); } } } } }} 定时执行SQL语句,在SQL SERVER服务器创建一个Job,定时执行就完事了。就执行条语句,没必要用windows服务。 楼主的贴图看其来像是windows service。因此楼主也可以做一个windows server,安装完之后,就是一个永驻运行的进程。如果设置成自动启动的话,那么即使机器重启了,它还是会继续运行。首先我们需要有一个文件,记录下一次执行sql的时间。然后在这个windows服务里面写一个timer,每天运行一次。每次都检查这个文件,如果到时间了,就执行。执行完毕之后,修改这个文件,改成下一次执行日期。 定时执行SQL语句问题//定于星期一执行SQL语句 public static string dt; public static string week; protected void Page_Load(object sender, EventArgs e) { //获取当前日期是星期几 dt = DateTime.Today.DayOfWeek.ToString(); //根据取得的星期英文单词返回汉字 switch (dt) { case "Monday": week = "星期一"; break; case "Tuesday": week = "星期二"; break; case "Wednesday": week = "星期三"; break; case "Thursday": week = "星期四"; break; case "Friday": week = "星期五"; break; case "Saturday": week = "星期六"; break; case "Sunday": week = "星期日"; break; } //利用TextBox文本框显示当前日为星期几 this.LblStr.Text = week; if(week == 星期一){ //实现SQL语句 } } 首先感谢楼上各位大神的回复其实好像是要用到Windows Server的(之前没接触过。。),也看了楼上大神提到的SQL计划任务所以有个疑惑,我这个执行SQL好像不是固定的,是要根据参数来的。我这里要做的功能是根据订单号,来判断是否超过7天了,超过7天了对该订单进行SQL的update操作。需要调用到的参数是订单号,这个是不固定的,所以不知道SQL计划任务还行不?我自己倒有一个想法,不知道行得通不?利用SQL语句的DATEDIFF方法首先判断日子?然后再进行规定的操作?不知道能行得通吗?谢谢继续指教。。 关于SOCKET的问题 winform程序怎么启动唯一运行实例? c# 代码实现关闭.exe文件问题 datagridview显示问题 如何给动态页传参 提问:如何得到WebBrowser中,某位置的像素的颜色? 串口问题! TCP网络编程达人来看看,很神奇 如何对不同级别的用户实现进入系统的控制???? 有谁知道C#中调用DLL的写法是怎么样的,和C++的是否一致? 怎么在c# 中,在webform1中点击一个button,弹出webform2 C#中,如何在窗体中循环播放图片?
這個控件
System.Timers.Timer timer = new System.Timers.Timer();我寫了個類型功能的小程式,我是用服務寫的,具體功能代號就不帖出來了using System;
using System.Collections.Generic;
using System.Threading;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.IO;
namespace DBTableDataTransServer
{
public partial class DBTableDataTransServer : ServiceBase
{
System.Timers.Timer timer = new System.Timers.Timer();
DataTable tbExecPlan = new DataTable();
int nTimes = 0;
bool IsCanExec = true;
bool IsExecOk = false;
string strCurrentTime = "";
string strExecSeq = "";
string strSeqMark = "";
string strExecTime = "";
bool IsOver = true;
public DBTableDataTransServer()
{
InitializeComponent();
} protected override void OnStart(string[] args)
{
// TODO: 在此处添加代码以执行開啟服务所需的操作。
//記錄服務開啟日誌
WebHp.WriteServiceLog("A");
//讀取配置文件
tbExecPlan = WebHp.LoadXml("B");
//設置每隔1分鍾發生一次事件
timer.Interval = 60000;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);
timer.Enabled = true;
timer.Start();
} protected override void OnStop()
{
// TODO: 在此处添加代码以执行停止服务所需的关闭操作。
//記錄服務開啟日誌
WebHp.WriteServiceLog("B");
} private void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{ //當前時間
strCurrentTime = DateTime.Now.ToString("yyyyMMddHHmm");
strExecSeq = "";//本次要執行的項次
strSeqMark = "";
nTimes = nTimes + 1;
//讀取計劃執行的日誌
//日誌格式,如0001項次
//按年:0001:2014 Begin at 201410101230
//按月: 0001:201410 Begin at 201410101230
//按天: 0001:20141001 Begin at 201410101230
try
{
if (IsCanExec && IsOver)
{
IsOver = false;
string strText = WebHp.ReadPlanExecLog();
if (strText == null)
{
strText = "";
}
else
{
if (strText == "Error")
{
IsCanExec = IsOver = true;
return;
}
}
for (int i = 0; i < tbExecPlan.Rows.Count; i++)
{
if (tbExecPlan.Rows[i]["IsOpen"].ToString() == "False")
{
continue;
}
strExecTime = tbExecPlan.Rows[i]["ExecTime"].ToString().Replace("-", "").Replace(":", "").Replace(":", "").Replace("-", "");
switch (tbExecPlan.Rows[i]["ExecType"].ToString().Substring(0,1))
{
case "A"://按年執行
if (strExecTime.CompareTo(strCurrentTime.Substring(4, 8)) <= 0)
{
if (strText.IndexOf(tbExecPlan.Rows[i]["Seq"] + ":" + strCurrentTime.Substring(0, 4)) >= 0)
{
continue;
}
strExecSeq = tbExecPlan.Rows[i]["Seq"].ToString();
strSeqMark = strExecSeq +":"+ strCurrentTime.Substring(0, 4) + " ";
}
break;
case "B"://按月執行
if (strExecTime.CompareTo(strCurrentTime.Substring(6, 6)) <= 0)
{
if (strText.IndexOf(tbExecPlan.Rows[i]["Seq"] + ":" + strCurrentTime.Substring(0, 6)) >= 0)
{
continue;
}
strExecSeq = tbExecPlan.Rows[i]["Seq"].ToString();
strSeqMark = strExecSeq + ":" + strCurrentTime.Substring(0, 6) + " ";
}
break;
case "C"://按天執行
if (strExecTime.CompareTo(strCurrentTime.Substring(8, 4)) <= 0)
{
if (strText.IndexOf(tbExecPlan.Rows[i]["Seq"] + ":" + strCurrentTime.Substring(0, 8)) >= 0)
{
continue;
}
strExecSeq = tbExecPlan.Rows[i]["Seq"].ToString();
strSeqMark = strExecSeq + ":" + strCurrentTime.Substring(0, 8) + " ";
}
break;
case "D"://循環執行
if (nTimes / 2 >= Convert.ToInt32(tbExecPlan.Rows[i]["ExecTime"].ToString()))
{
nTimes = 0;
strExecSeq = tbExecPlan.Rows[i]["Seq"].ToString();
strSeqMark = strExecSeq + ":" + strCurrentTime.Substring(0, 10) + " ";
}
break;
}
if (strExecSeq != "")
{
break;
}
}
if (strExecSeq != "")
{
//寫入執行開始日誌
strText = strSeqMark + " 開始執行時間:" + strCurrentTime + "->" + strExecSeq + ":End";
string strRe=WebHp.WritePlanExecLog(strText, "A"); //開始執行此項次
IsCanExec = false;
strRe = WebHp.TransData(strExecSeq, "B");
if (strRe.Substring(strRe.Length - 2, 2) == "OK")
{
IsExecOk = true;
}
IsCanExec = true;
}
IsOver = true;
}
}
catch (System.Exception ex)
{
IsOver = true;
IsCanExec = true;
}
finally
{
if (strExecSeq != "")
{
if (IsExecOk)
{
WebHp.WritePlanExecLog(strExecSeq + ":End", "B");
}
else
{
WebHp.WritePlanExecLog(strExecSeq + ":End", "C");
}
}
}
}
}
}
就执行条语句,没必要用windows服务。
然后在这个windows服务里面写一个timer,每天运行一次。每次都检查这个文件,如果到时间了,就执行。执行完毕之后,修改这个文件,改成下一次执行日期。
//定于星期一执行SQL语句
public static string dt;
public static string week;
protected void Page_Load(object sender, EventArgs e)
{
//获取当前日期是星期几
dt = DateTime.Today.DayOfWeek.ToString();
//根据取得的星期英文单词返回汉字
switch (dt)
{
case "Monday":
week = "星期一";
break;
case "Tuesday":
week = "星期二";
break;
case "Wednesday":
week = "星期三";
break;
case "Thursday":
week = "星期四";
break;
case "Friday":
week = "星期五";
break;
case "Saturday":
week = "星期六";
break;
case "Sunday":
week = "星期日";
break;
}
//利用TextBox文本框显示当前日为星期几
this.LblStr.Text = week;
if(week == 星期一){
//实现SQL语句
}
}
所以有个疑惑,我这个执行SQL好像不是固定的,是要根据参数来的。
我这里要做的功能是根据订单号,来判断是否超过7天了,超过7天了对该订单进行SQL的update操作。需要调用到的参数是订单号,这个是不固定的,所以不知道SQL计划任务还行不?
我自己倒有一个想法,不知道行得通不?利用SQL语句的DATEDIFF方法首先判断日子?然后再进行规定的操作?不知道能行得通吗?谢谢继续指教。。