Reputation: 2642
I have a class library, use for select the record in Ms. Access and then take the selected record to insert in SQL Server.
public class SyncDatabase
{
private static SyncDatabase objs = null;
public static SyncDatabase GetInstance
{
get
{
if (objs == null)
objs = new SyncDatabase();
return objs;
}
}
OleDbConnection con = new OleDbConnection("Provider=Microsoft.jet.oledb.4.0; data source=C:/Users/cheata/Desktop/TimeSheet.mdb");
public void GetData()
{
con.Open();
OleDbCommand cmd = new OleDbCommand("select * from tblEmployee_TimeSheet", con);
DataTable dt = new DataTable();
dt.Load(cmd.ExecuteReader());
var _data = dt.AsEnumerable().Select(row => new tblEmployee_TimeSheet
{
ID = row.Field<int>(0),
EID = row.Field<int>(1),
CheckIn = row.Field<DateTime>(2),
CheckOut = row.Field<DateTime>(3),
DateCheck = row.Field<DateTime>(4)
}).ToList();
con.Close();
var context = TimeSheetDataContext.GetInstance;
foreach (tblEmployee_TimeSheet obj in _data)
{
context.Insert<tblEmployee_TimeSheet>(obj);
}
}
}
I have one window service that written with the thread, When the window is start up, MyWindowService also start. And then it will call the thread OnStart
.
What I want is to call SyncDatabase
class library in my thread OnStart
, and then check in the OnStart
method, whether the
value that I want to insert is already exist in the record of SQL Server or not yet.
This is my thread :
public partial class Thread : ServiceBase
{
public Thread()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
}
protected override void OnStop()
{
}
}
Could any one tell me how to do that please.
Thanks in advanced.
Upvotes: 0
Views: 191
Reputation: 10840
OnStart method is called when start command is sent to the service and it should be light enough to finish quickly. It is better to start a thread or timer in OnStart and return from method as quickly as possible. Coding in service is similar to coding in any other kind of application in .net. Add the reference of the assembly containing SynDataBase class and then use it same as you have used in your console or win app. You can also provide config file in service and if your code takes connection string from config, you can provide that config element in configuration file here in service project. Just a rough code to show you how to do that.Write this code in your OnStart method
System.Threading.Thread thread = new System.Threading.Thread(() =>
{
SyncDatabase.GetInstance.DoWhatYouWant();
});
thread.Start();
Upvotes: 1
Reputation: 3929
you can make a stored procedure that check existence of object insert if it exists return -1 if not return 0
you have only to call it from in your loop
Upvotes: 1