<pre lang="msil">using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.ServiceProcess; using System.Collections.Specialized; using System.Configuration; using System.Data.SqlClient; using System.Threading; public class Lite : System.ServiceProcess.ServiceBase { private System.Diagnostics.EventLog LiteEventLog; private IContainer components; NameValueCollection appSettings = ConfigurationManager.AppSettings; static TimerCallback timerDelegate; static Timer timer; public Lite() { InitializeComponent(); if(!System.Diagnostics.EventLog.SourceExists("LiteSource")) { System.Diagnostics.EventLog.CreateEventSource("LiteSource","Lite"); } LiteEventLog.Source = "LiteSource"; LiteEventLog.Log = "Lite"; } static void Main() { System.ServiceProcess.ServiceBase[] ServicesToRun; ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Lite()}; System.ServiceProcess.ServiceBase.Run(ServicesToRun); } private void InitializeComponent() { this.LiteEventLog = new System.Diagnostics.EventLog(); ((System.ComponentModel.ISupportInitialize)(this.LiteEventLog)).BeginInit(); this.LiteEventLog.Log = "Application"; this.LiteEventLog.Source = "LiteSource"; this.ServiceName = "Lite"; ((System.ComponentModel.ISupportInitialize)(this.LiteEventLog)).EndInit(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); timer.Dispose(); } } base.Dispose( disposing ); timer.Dispose(); } protected override void OnStart(string[] args) { LiteEventLog.WriteEntry(" Lite started."); Lite lite = new Lite(); AutoResetEvent autoEvent = new AutoResetEvent(false); timerDelegate = new TimerCallback(lite.OnTimedEvent); timer = new Timer(timerDelegate, autoEvent, 1000, 1000); autoEvent.WaitOne(); } protected override void OnStop() { LiteEventLog.WriteEntry(" Lite stoped."); timer.Dispose(); } protected override void OnContinue() { LiteEventLog.WriteEntry(" Lite working."); } public void OnTimedEvent(object source) { SqlConnection Sqlca; string stmt = appSettings["sqlStmt"]; try { string connectString = ConfigurationManager.ConnectionStrings["myConn"].ConnectionString; if (null != connectString) { SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectString); Sqlca = new SqlConnection(builder.ConnectionString); Sqlca.Open(); SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(stmt, Sqlca); DataSet ds_location = new DataSet(); DataTable dt_loc = new DataTable("Table_1"); ds_location.DataSetName = "Dataset_1"; mySqlDataAdapter.Fill(dt_loc); ds_location.Tables.Add(dt_loc); dt_loc.Columns[0].ColumnMapping = MappingType.Attribute; dt_loc.Columns[1].ColumnMapping = MappingType.Attribute; ds_location.WriteXml("Sample.xml"); Sqlca.Close(); } } catch (SqlException sqlEX) { Console.WriteLine(sqlEX); } } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)