Windows服务未连接到数据库

时间:2012-04-26 22:29:43

标签: c# windows c#-4.0 windows-services timer

我有一个Windows服务。它在每隔18秒连接并检查我的数据库,然后向外部客户端发送soap消息。 我已成功在我的计算机上安装了该服务,但它没有做任何特别的事情,也没有填写我的服务器日志中的任何错误。

如何找出出错的地方?

更新

protected override void OnStart(string[] args)
    {
        timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
        timer.Interval = 15000;
        timer.Enabled = true;
        timer.Start();
        eventLog1.WriteEntry("Windows Service 3 Started");
    }

    protected override void OnStop()
    {
        eventLog1.WriteEntry("Windows Service 3 Stopped");

    }

我的RunAPP()是我通过存储过程连接到数据库的地方。

对我的问题的任何见解都会很棒!

  private void RunApp()
    {

        p.Start();
        string connStr = ConfigurationManager.ConnectionStrings["bbsConnectionString"].ConnectionString;
        try
        {
            SqlConnection Con = new SqlConnection(connStr);
            Con.Open();
            SqlCommand cmd = new SqlCommand("AvailableChanges", Con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter NewSysChangeVersionParam = new SqlParameter("@NewSysChangeVersion", SqlDbType.Int);
            NewSysChangeVersion.Value = (object)NewSysChangeVersion ?? DBNull.Value;
            NewSysChangeVersion.Direction = ParameterDirection.InputOutput;
            NewSysChangeVersion.SqlDbType = SqlDbType.BigInt;
            SqlDataReader sdr = cmd.ExecuteReader();
            int sdrreader = sdr.FieldCount;
            InventoryPushSubscriptionRecord rec = new InventoryPushSubscriptionRecord();


            while (sdr.Read())
            {

                inrec.InventoryPushSubscriptionId = sdr.GetInt32(0);
                inrec.SysChangeVersion = sdr.IsDBNull(1) ? (long?)null : sdr.GetInt64(1);
                inrec.InvDate = sdr.GetDateTime(2);
                inrec.ResortId = sdr.GetInt32(3);
                inrec.RoomType = sdr.GetString(4);
                inrec.InvCount = sdr.GetInt32(5);
                inrec.ResortName = sdr.GetString(6);


                Int64 NewSysChangeVersion;
                NewSysChangeVersion = Convert.ToInt64(rec.LastSysChangeVersion);
                int ResortId;
                ResortId = inrec.ResortId;
                string RoomType;
                RoomType = inrec.RoomType;
                int avail = inrec.InvCount;
                DateTime frodte = inrec.InvDate;
                DateTime todte = inrec.InvDate;
                int NoofRatePackages = sdrreader;
                Int32 InventoryPushSubscriptionId;
                InventoryPushSubscriptionId = inrec.InventoryPushSubscriptionId;
                Int64 NewLastSysChangeVersion;
                NewLastSysChangeVersion = Convert.ToInt64(inrec.SysChangeVersion);
                if (NewSysChangeVersion != null)
                {
                    sendUpdate(NewSysChangeVersion, ResortId, RoomType, avail, frodte, todte, NoofRatePackages, InventoryPushSubscriptionId, NewLastSysChangeVersion);
                }
            }

            //    sdr.Close();
            //    sdr.Dispose();

        }

        catch (Exception ex)
        {

更新                eventLog1.WriteEntry( “异常”);                扔;             }         }

1 个答案:

答案 0 :(得分:0)

按照建议编辑我的例外情况。

catch(Exception ex)             {

            eventLog1.WriteEntry(ex.Message);
            eventLog1.WriteEntry(ex.StackTrace);
            throw;

        }