最近做一个程序,客户要求加在到服务中,这样确保只要系统运行,程序就执行,可是我加载到服务以后,怎么样也连接不到数据源,不知道是不是我的手法有问题,我用的是timer控件,这是代码,希望各位高手能找出问题急啊using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Text;
using System.Data.SqlClient;
using System.Data.Odbc;
using System.Timers;
using System.IO;
using System.Xml;namespace PISelect
{
    partial class Service1 : ServiceBase
    {
        System.Timers.Timer timer1 = new System.Timers.Timer();
        SqlConnection con = new SqlConnection("Data Source=10.20.3.55;Initial Catalog=spc;User ID=sa;Password=123456");
        SqlCommand com=new SqlCommand();
        OdbcConnection conn=new OdbcConnection();         public Service1()
        {
            InitializeComponent();
           
            this.timer1.Enabled = true;
            this.timer1.Interval = 20000;
            this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Tick);
        }
        static void Main()
        {
            System.ServiceProcess.ServiceBase[] ServicesToRun;
            ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service1() };
            System.ServiceProcess.ServiceBase.Run(ServicesToRun);        }
        private const string FILE_NAME = "D:\\PI\\writelog.txt";        public static void writeLog(string sql)
        {
            if (!File.Exists(FILE_NAME))
            {
                StreamWriter sr = File.CreateText(FILE_NAME);
            }
            StreamWriter sr2 = File.AppendText(FILE_NAME);
            sr2.WriteLine("\n");
            sr2.WriteLine("======================" + DateTime.Now.ToString() + "====================");
            sr2.WriteLine(DateTime.Now.ToString() + sql);
            sr2.Close();
        }        protected override void OnStart(string[] args)
        {
            timer1.Start();
            if (con.ToString() == "") 
            {
                con = new SqlConnection("Data Source=10.20.3.55;Initial Catalog=spc;User ID=sa;Password=123456");
            }
            if (conn.ToString() == "")
            {
                conn = new OdbcConnection(); 
            }
        }
        protected override void OnStop()
        {
            timer1.Stop();
            conn.Dispose();
        }        private void timer1_Tick(object sender, EventArgs e)
        {
            //判断临时表是否有数据等待插入正式表
            string str = "select * from  pitemp";
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            
            com.CommandText=str;
            com.Connection=con;
            SqlDataReader read = com.ExecuteReader();
            if (read.Read())
            {
                read.Close();
                this.select();
            }
            else
            {
                read.Close();
            }
        }        public void select()
        {
            string id = "";
            string CPName = "";
            string InstrumentName = "";
            string CIName = "";
            string CIType = "";
            string CIValue = "";
            string CIMin = "";
            string CIMax = "";
            DateTime CITime = DateTime.Now; ;
            string PICode = "";
            string PIValue = "";
            //查询临时表
            string SelectStr = "select top 1* from  pitemp";
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }            writeLog(SelectStr);            SqlCommand SelectCom = new SqlCommand(SelectStr, con);
            SqlDataReader SelectDR = SelectCom.ExecuteReader();
            if (SelectDR.Read())
            {
                id = SelectDR["id"].ToString();
                CPName = SelectDR["CPName"].ToString();
                InstrumentName = SelectDR["InstrumentName"].ToString();
                CIName = SelectDR["CIName"].ToString();
                CIType = SelectDR["CIType"].ToString();
                CIValue = SelectDR["CIValue"].ToString();
                CIMin = SelectDR["CIMin"].ToString();
                CIMax = SelectDR["CIMax"].ToString();
                if (SelectDR["CITime"].ToString() != "")
                {
                    CITime = Convert.ToDateTime(SelectDR["CITime"]);
                }
                PICode = SelectDR["PICode"].ToString();
            }
            SelectDR.Close();            string State = "未处理";
            //插入正式表          
            string InsertStr = "insert pitable(CPName,InstrumentName,CIName,CIType,CIValue,CIMin,CIMax,CITime,PICode,State)";
            InsertStr += "values('" + CPName + "','" + InstrumentName + "','" + CIName + "','" + CIType + "','" + CIValue + "','" + CIMin + "','" + CIMax + "','" + CITime + "','" + PICode + "','" + State + "')";            writeLog(InsertStr);            SqlCommand InsertCom = new SqlCommand(InsertStr, con);
            InsertCom.ExecuteNonQuery();
            //删除临时表内容
            string DeleteStr = "delete from pitemp where id='" + id + "'";            writeLog(DeleteStr);            SqlCommand DeleteCom = new SqlCommand(DeleteStr, con);
            DeleteCom.ExecuteNonQuery();            con.Close();
            //从PI表提取值
            try
            {                //conn.ConnectionString = "server=10.20.76.3;dsn=pi;port=5450;uid=pidemo;pwd=";// ConnectStr();
                string constr = "server=10.20.131.3;dsn=pi;port=5450;uid=pidemo;pwd=";
                OdbcConnection conn = new OdbcConnection(constr);
                if (conn.State != ConnectionState.Open)
                {
                    conn.Open();//这里总是执行不过直接报错到catch了,catch捕获的异常就是
                                      //ERROR [IM002] [Microsoft][ODBC 驱动程序管理器] 未发现数据源名称并且未指定默认驱动程序

                }
                string PIStr = "SELECT value FROM piavg WHERE (piavg.tag='" + PICode + "') AND time >='" + (CITime.AddDays(-1)) + "'";                writeLog(PIStr);                OdbcCommand PICom = new OdbcCommand(PIStr, conn);
                OdbcDataReader PIDR = PICom.ExecuteReader();
                while (PIDR.Read())
                {
                    PIValue = PIDR["value"].ToString();
                }
                PIDR.Close();
                conn.Close();
            }
            catch(Exception e)
            {
                PIValue = "07083";
                writeLog(e.Message);
                conn.Close();
            }
           
            
            //更新正式表PI值           
            string UpdateStr = "update pitable set PIValue='" + PIValue + "'where PICode='" + PICode + "'and CITime='" + CITime + "'";            writeLog(UpdateStr);
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            SqlCommand UpdateCom = new SqlCommand(UpdateStr, con);
            UpdateCom.ExecuteNonQuery();
            con.Close();
        }
    }
}