Esempio n. 1
0
        protected override void OnStart(string[] args)
        {
            //This service is set in the ProjectInstaller as a dependant on MSSQL$SQLEXPRESS
            //This service is set to start automatically after reboot
            try
            {
                #region Check for database(SeeCommon) - attach to sql server if necessary

                SqlBase sqlBase = new SqlBase();
                sqlBase.SetDb("SeeCommon");
                //Database .mdf and .ldf files are copied to the application directory as part of installation
                //Copy the files and attach the database to the server
                WriteAppLogInfo("Service Starting: Master Connection string = " + Environment.NewLine +
                                sqlBase.MasterConnectionString + Environment.NewLine +
                                "SeeCommon Connection string = " + Environment.NewLine +
                                sqlBase.ConnectionString, 1111);
                if (!sqlBase.IsDbRegistered())
                {
                    string datFilePath1 = Application.StartupPath + @"\SeeCommon.mdf";
                    string logFilePath1 = Application.StartupPath + @"\SeeCommon_log.ldf";
                    string datFilePath2 = DatabasePath + "SeeCommon.mdf";
                    string logFilePath2 = DatabasePath + "SeeCommon_log.ldf";
                    File.Copy(datFilePath1, datFilePath2, true);
                    File.Copy(logFilePath1, logFilePath2, true);
                    sqlBase.ExAttachDB(datFilePath2, logFilePath2, "SeeCommon");
                    sqlBase.Connection.Open();//Test the connection or fail
                }

                #endregion
                int sysCnt = FillSysDefsTable();     //Get the currently defined systems
                foreach (SeeCommonDataSet.SysDefsRow defRow in DefsTable)
                {
                    CreateNewSystem(defRow, true);
                }
                SendUdpBroadcast(new UdpMessage(1, DateTime.Now, "All", "Service Started"));
                controlTimer.Start();   //This timer checks for broken connections
            }
            catch (Exception exc)
            { WriteAppLogError(exc.Message + Environment.NewLine + exc.ToString(), 10000); }
        }