Merhaba arkadaşlar ben bir windows service yazdım 30 saniyede bir, database e kayıt eklemesi lazım normalde herhangi bir hata almadım ama database e herhangi bir kayıt eklemiyor. Ve Dal katmanımı console application üzerinde denedim çalışıyor herhangi bir sıkıntı yok ama nedense windows service kayıt eklemiyor.
DAL katmanım:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WindowsService
{
public class DAL
{
SqlConnection con;
SqlCommand cmd;
int returnValues;
public DAL()
{
con = new SqlConnection("data source=DESKTOP-A1V7ASI\\SQLEXPRESS; initial catalog = WindowsService; Integrated Security=true;");
}
public int OlayEkle(string Olay, DateTime OlusmaTarihi)
{
try
{
cmd = new SqlCommand("insert into Olaylar (Olay,OlusmaTarihi) values (@Olay,@OlusmaTarihi)", con);
cmd.Parameters.Add("@Olay", SqlDbType.NVarChar).Value = Olay;
cmd.Parameters.Add("@OlusmaTarihi", SqlDbType.DateTime).Value = OlusmaTarihi;
con.Open();
returnValues = cmd.ExecuteNonQuery();
}
catch (Exception)
{
}
finally
{
con.Close();
}
return returnValues;
}
}
}
Service1 class ım:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace WindowsService
{
public partial class Service1 : ServiceBase
{
public Timer t;
public Service1()
{
InitializeComponent();
t = new Timer(30000);
t.Elapsed += T_Elapsed;
}
private void T_Elapsed(object sender, ElapsedEventArgs e)
{
DAL dal = new DAL();
dal.OlayEkle("Olay çalıştı", DateTime.Now);
}
protected override void OnStart(string[] args)
{
t.Start();
}
protected override void OnPause()
{
t.Stop();
}
protected override void OnContinue()
{
t.Start();
}
protected override void OnShutdown()
{
t.Stop();
}
protected override void OnStop()
{
t.Stop();
}
}
}