タイマー付きの Windows サービス プログラムがあります。経過したイベントはデフォルトのテストから発生しますが、DB は更新されません。同じ正確なコードが、Windows フォームのボタン クリック イベントで機能します。コードはこちらです。3 層アーキテクチャと sql サーバー 2012 を使用します。
namespace DALStations
{
class GetUpdate
{
DALConnection connectstring;
public GetUpdate()
{
connectstring = new DALConnection();
}
public void UpdateCommands() //SqlCommand
{
SqlCommand authorize = new SqlCommand();
authorize.CommandText = "dbo.UpdateTest";
authorize.CommandType = CommandType.StoredProcedure;
authorize.Connection = connectstring.GetConnection();
authorize.Connection.Open();
authorize.ExecuteNonQuery();
authorize.Connection.Close();
}
}
}
バル
namespace BALStations
{
class BAL
{
//public void UpdateTest()
//{
// new DALTest().Updatetest();
//}
public void CallUpdateProcedure()
{
new GetUpdate().UpdateCommands();
}
}
}
Service Layer or UI
namespace Stations
{
public partial class Service1 : ServiceBase
{
private static System.Timers.Timer aTimer;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
Process();
}
public void Process()
{
aTimer = new System.Timers.Timer(10000);
// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(timer1_Elapsed);
// Set the Interval to 30 seconds (30000 milliseconds).
aTimer.Interval = 3000;
aTimer.Enabled = true;
using (StreamWriter writer = new StreamWriter(@"C:\Users\Joe\Desktop\Summit Works\Authorized Timer\Details.txt", true))
{
writer.WriteLine("Service Start {0}", DateTime.Now, true);
}
aTimer.Start();
}
protected override void OnStop()
{
this.timer1.Enabled = false;
}
private void timer1_Elapsed(object sender,
System.Timers.ElapsedEventArgs e)
{
BAL bal = new BAL();
bal.CallUpdateProcedure();
}
}
}