【问题标题】:Schedule Windows Service to run once daily安排 Windows 服务每天运行一次
【发布时间】:2014-06-01 09:26:19
【问题描述】:

我有一个用 SQL Server 2008 R2 编写的存储过程,需要每天运行。存储过程接受两个日期start dateend date 作为参数,并根据这些日期从table A 获取数据并将其写入table B

为了自动化每天运行存储过程的任务,我写了一个Windows服务。我在Service1.cs 中安排了我的任务如下:

System.Timers.Timer oTimer = null;

public ServiceExample()
{
   InitializeComponent();
   oTimer = new System.Timers.Timer();
   SetTimer();
}

private void SetTimer()
{
   DateTime currentTime = DateTime.Now;
   int intervalToElapse = 0;
   DateTime scheduleTime = new DateTime(currentTime.Year, currentTime.Month, currentTime.Day, 8, 0, 0); //run at 8 am every morning

   if (currentTime <= scheduleTime)
      intervalToElapse = (int)scheduleTime.Subtract(currentTime).TotalSeconds;
   else
      intervalToElapse = (int)scheduleTime.AddDays(1).Subtract(currentTime).TotalSeconds;

   oTimer = new System.Timers.Timer(intervalToElapse);
   oTimer.Elapsed += new System.Timers.ElapsedEventHandler(oTimer_Elapsed);

   oTimer.Start();
}

void oTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
   //make connection to SQL and call the SP
   DBLibrary oDBLibrary = new DBLibrary();
   DataSet dsCustomer = oDBLibrary.getCustomerDetails();
   oTimer.Interval = (24 * 60 * 60 * 1000);
}

protected override void OnStart(string[] args)
{
   SetTimer();
}

protected override void OnStop()
{
   oTimer.Stop();
}

对存储过程的调用如下:

public DataSet getCustomerDetails()
{
    DateTime First_Date = DateTime.Now;
    DateTime dateOnly = First_Date.Date;
    DateTime First_Date1 = dateOnly.AddDays(-1); // assign back to see the new instance
    DateTime End_Date = DateTime.Now;

    SqlConnection oSqlConnection = new SqlConnection(strConn);
    oSqlConnection.Open();

    SqlCommand oSqlCommand = new SqlCommand();
    oSqlCommand.CommandTimeout = 0;
    oSqlCommand.CommandType = CommandType.StoredProcedure;
    oSqlCommand.CommandText = "Daily_Airtime_Summary";

    oSqlCommand.Parameters.Add(new SqlParameter("@StartDate", SqlDbType.DateTime)).Value = First_Date1;
    oSqlCommand.Parameters.Add(new SqlParameter("@EndDate", SqlDbType.DateTime)).Value = End_Date;
    oSqlCommand.Connection = oSqlConnection;

    DataSet ds = new DataSet();
    SqlDataAdapter oSqlDataAdapter = new SqlDataAdapter(oSqlCommand);
    oSqlDataAdapter.Fill(ds);

    oSqlConnection.Close();
    return ds;
 }

我面临的问题:

  1. 即使预定时间是上午 8 点,我在上午 10 点左右调试我的服务,它也会立即开始运行(执行存储过程)。
  2. 这个想法是调用/运行存储过程once。但是,当我查看table B 时,我看到相同的数据被填充了近 17 次。
  3. 参数中传递的日期是start date = yesterday's dateend date = today's date。但是,我看到程序在获取start date = yesterday's dateend date = today's date 的日期后(多次)持续运行start date = today's date

有人可以在这里指导吗?

【问题讨论】:

  • 您的参数不依赖于服务中的数据,因此您只需在 SQL Server 上创建作业。
  • 您在哪里跟踪某一天是否已拨打电话?您需要以某种方式将该信息存储在某处并检查它......另外:如果您每天只运行一次,我就不会遇到创建服务的麻烦 - 只需创建一个 控制台应用程序 您可以使用 Windows 计划任务安排每天运行一次.... 更简单...
  • @marc_s 我会研究 console application 但目前我想先让它工作。我的问题是为什么存储过程在同一个实例中被多次调用,而不是在存储过程执行后服务转到sleep
  • 只使用任务计划程序或 SQL Server 代理怎么样? Windows 服务很复杂。
  • 使用该@usr 的任何指针?解决Windows Service 问题不走运...

标签: sql stored-procedures sql-server-2008-r2 windows-services scheduled-tasks


【解决方案1】:

检查您的逻辑以了解运行 proc 的时间。这可能没有正确初始化,导致 proc 在初始启动时运行。

另外,要调试它,您需要使用 . Net 实用程序,将服务可执行文件指向项目中的调试版本。在调试模式下编译,在控制面板中使用 Windows 服务实用程序启动服务,然后在 Visual Studio 中附加到该进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 2010-10-29
    • 1970-01-01
    • 2023-04-08
    • 2010-10-04
    • 2014-07-18
    相关资源
    最近更新 更多