我写的一个windows服务程序

主要是调用一个存储过程,在特定的时候查出一个报表,并输出XML。挺简单的。

 

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.Data.SqlClient;
using System.Xml;
using System.IO;
using System.Collections;

namespace StockUpService
{
    public partial class StockUpService : ServiceBase
    {
        public StockUpService()
        {
            InitializeComponent();

            if (!System.Diagnostics.EventLog.SourceExists("MySource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MySource", "MyNewLog");
            }
            eventLog1.Source = "MySource";
            eventLog1.Log = "MyNewLog";
        }

        protected override void OnStart(string[] args)
        {
            System.Timers.Timer t = new System.Timers.Timer(50000);             //实例化Timer类,设置间隔时间为50000毫秒|50秒执行一次;
            t.Elapsed += new System.Timers.ElapsedEventHandler(runservice);         //到达时间的时候执行事件;
            t.AutoReset = true;                                                 //设置是执行一次(false)还是一直执行(true);
            t.Enabled = true;                                                   //是否执行System.Timers.Timer.Elapsed事件;
        }
        public void runservice(object source, System.Timers.ElapsedEventArgs e)
        {
            string t;
            t = System.DateTime.Now.Hour.ToString()+System.DateTime.Now.Minute.ToString();
            if (t == "51")
            {
                readXml();
            }
        }

        public static void readXml()
        {
            string classId;
            string filialeId;
            string startTime;
            string endTime;
            string companyType;
            int timeInterval;
            string aa = "";
            string xmlChildNodes = "";
            //string dsn = "server=(local);database=keede1228;user id=sa;password=123;";
            string dsn;           

            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("d:\\StockUpConfig.xml");
            XmlNode xn = xmlDoc.SelectSingleNode("stockwarning");
            XmlNodeList xnl = xn.ChildNodes;

            XmlNode xn3 = xmlDoc.SelectSingleNode("stockwarning/goodsclass");
            XmlNodeList xn3l = xn3.ChildNodes;


            foreach (XmlNode xnf in xnl)
            {
                XmlElement xe = (XmlElement)xnf;
                XmlNodeList xnf1 = xe.ChildNodes;
                xmlChildNodes = xmlChildNodes + xnf.InnerText + "|";
            }

            string[] cc = xmlChildNodes.Split('|');
            string outputpath;
            filialeId = cc[1];
            companyType = cc[2];
            timeInterval = Convert.ToInt16(cc[3]);
            outputpath = cc[4];
            dsn = cc[5];
            endTime = DateTime.Now.Date.ToShortDateString();
            startTime = DateTime.Now.AddDays(-timeInterval).ToShortDateString();
            string className;
            int po = 1;
            SqlConnection connection = new SqlConnection(dsn);

            foreach (XmlNode xn3f in xn3l)
            {
                XmlNode xn2 = xmlDoc.SelectSingleNode("stockwarning/goodsclass/group" + po);
                XmlNodeList xn2l = xn2.ChildNodes;
                XmlElement xe = (XmlElement)xn3f;
                className = xe.GetAttribute("classname");
                po++;
                DataSet ds = new DataSet();
                foreach (XmlNode xn2f in xn2l)
                {
                    aa = xn2f.InnerText;

                    connection.Open();
                    classId = aa;
                    SqlCommand command = connection.CreateCommand();
                    command.CommandText = "P_Raifei_GetClassGoodsStockUp";
                    command.CommandType = CommandType.StoredProcedure;
                    SqlParameter param1 = new SqlParameter("@ClassId", classId);
                    SqlParameter param2 = new SqlParameter("@FilialeId", filialeId);
                    SqlParameter param3 = new SqlParameter("@StartTime", startTime);
                    SqlParameter param4 = new SqlParameter("@EndTime", endTime);
                    SqlParameter param5 = new SqlParameter("@CompanyType", companyType);
                    command.Parameters.Add(param1);
                    command.Parameters.Add(param2);
                    command.Parameters.Add(param3);
                    command.Parameters.Add(param4);
                    command.Parameters.Add(param5);

                    SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
                    reader.Close();

                    string select = "P_Raifei_GetClassGoodsStockUp" + " " + "'" + classId + "'," + "'" + filialeId + "'," + "'" + startTime + "',"
                        + "'" + endTime + "'," + "'" + companyType + "'";
                    SqlDataAdapter da = new SqlDataAdapter(select, connection);
                    int abc = 1;
                    string tbname;
                    string stockUpName;
                    stockUpName = outputpath + "StockUp" + DateTime.Now.Date.ToShortDateString() + "-" + className + ".xml";
                    tbname = "sp" + abc;
                    abc++;
                    da.Fill(ds, tbname);
                    ds.WriteXml(stockUpName, XmlWriteMode.WriteSchema);

                }
            }
            connection.Close();
        }

        protected override void OnStop()
        {
            eventLog1.WriteEntry("In onStop.");
        }

        protected override void OnContinue()
        {
            eventLog1.WriteEntry("In OnContinue.");
        } 

    }
}

posted @ 2010-01-30 09:40  nevgip  阅读(440)  评论(2编辑  收藏  举报