?? inventoryservice.cs
字號:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Data.SqlClient;
using System.IO;
using System.Timers;
namespace WindowsService1
{
public class InventoryService : System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private Timer tmrTimer;
public InventoryService()
{
// This call is required by the Windows.Forms Component Designer.
InitializeComponent();
// TODO: Add any initialization after the InitComponent call
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
// More than one user Service may run within the same process. To add
// another service to this process, change the following line to
// create a second service object. For example,
//
// ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
//
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new InventoryService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// InventoryService
//
this.ServiceName = "InventoryService";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// <summary>
/// Set things in motion so your service can do its work.
/// </summary>
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
// TODO: Add code here to start your service
if (tmrTimer == null)
{
tmrTimer = new Timer();
tmrTimer.Elapsed += new ElapsedEventHandler(OnTimer);
//Let the timer trigger once a minute
tmrTimer.Interval = 60000;
tmrTimer.Enabled = true;
}
else
{
tmrTimer.Start();
}
}
/// <summary>
/// Stop this service.
/// </summary>
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
if(tmrTimer != null)
tmrTimer.Stop();
}
public static void OnTimer(Object source,System.Timers.ElapsedEventArgs e)
{
InventorySystem.connect();
}
}
public class InventorySystem
{
static SqlConnection objSqlConnection ;
static SqlDataReader objSqlDataReader;
static SqlCommand objSqlCommand;
public static void connect()
{
try
{
string str = "Server=sam;Database=Inventory;User Id=sa;Password=;";
objSqlConnection = new SqlConnection(str);
objSqlConnection.Open();
objSqlCommand = new SqlCommand("Select ItemNo,Description,Quantity,ReOrderLevel from Item",objSqlConnection);
objSqlDataReader = objSqlCommand.ExecuteReader();
while(objSqlDataReader.Read())
{
if(Convert.ToInt32(objSqlDataReader[2].ToString()) <= Convert.ToInt32(objSqlDataReader[5].ToString()))
{
FileStream objFileStream = new FileStream(@"c:\Log.Txt",FileMode.OpenOrCreate,FileAccess.Write);
StreamWriter objStreamWriter = new StreamWriter(objFileStream);
objStreamWriter.BaseStream.Seek(0,SeekOrigin.End);
objStreamWriter.WriteLine(objSqlDataReader[0].ToString()+","+objSqlDataReader[1].ToString()+","+objSqlDataReader[3].ToString());
objStreamWriter.Flush();
objFileStream.Close();
}
}
objSqlConnection.Close();
}
catch(Exception ex)
{
throw ex;
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -