首页  ·  知识 ·  编程语言
在特定的时间调用存储过程,并把查询结果输出到XML文件中
网友   http://blog.csdn.net/liuxiuming/  .NET  编辑:德仔   图片来源:网络
using System; using System.Collections.Generic; using System.Component
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 == "177")  
            {  
                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;";  
            SqlConnection connection = new SqlConnection(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];  
            endTime = DateTime.Now.Date.ToShortDateString();  
            startTime = DateTime.Now.AddDays(-timeInterval).ToShortDateString();  
            string className;  
            int po = 1;  
            //int stockUpNameId = 0;  
            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++;  
                //stockUpNameId++;  
                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.");  
        }    
 
    }  

 
本文作者:网友 来源: http://blog.csdn.net/liuxiuming/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读