Я только что написал программу для запуска в качестве приложения-службы Windows, GasMeterMonitoring; однако, когда я запускаю это приложение на своем локальном компьютере, ничего не происходит вообще.
Программа должна собирать данные из базы данных MS Access на другом компьютере, а затем вставлять эти данные в базу данных Oracle.
Я не знаю, почему Это не работает. Любые идеи относительно того, что заставило бы это не работать должным образом? Я создал сервис, очень похожий на этот, до этого работал просто отлично.
Разница в том, что все данные были расположены на одной машине, где в этом коде есть две отдельные машины. Также я использую таймеры с этим кодом. Для настройки я использовал следующее руководство по использованию приложения-службы Microsoft и адаптацию кода таймера .
Я просто очень прошу помощи, чтобы разобраться со всем этим.
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.Timers;
using System.Data.Odbc;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.IO;
namespace GasMeterMonitoring
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
if(!System.Diagnostics.EventLog.SourceExists("GasMeterSourse"))
System.Diagnostics.EventLog.CreateEventSource("GasMeterSourse", "MeterLog");
eventLog1.Source = "GasMeterSourse";
eventLog1.Log = "MeterLog";
}
protected override void OnStart(string[] args)
{
eventLog1.WriteEntry("GasMeterMonitoring is reporting.");
timer1.Interval = 60000;
timer1.Enabled = true;
timer1.Start();
}
protected override void OnContinue()
{
eventLog1.WriteEntry("GasMeterMonitoring is reporting again");
}
protected override void OnPause()
{
eventLog1.WriteEntry("GasMeterMonitoring reporting has been paused");
}
protected override void OnStop()
{
timer1.Enabled = false;
eventLog1.WriteEntry("GasMeterMonitoring reporting has stopped");
}
protected override void OnShutdown()
{
eventLog1.WriteEntry("GasMeterMonitoring reporting has stopped due to computer shutdown");
}
public void EmailError(string ErrorMessage)
{
try
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("ladante.riley@severstalna.com");
message.Subject = "An error occured in GasMeterMonitoring.exe service";
message.From = new System.Net.Mail.MailAddress("ladante.riley@severstalna.com");
message.Body = ErrorMessage;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("DBN-EX03.severstalco.com");
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
smtp.Send(message);
}
finally
{
// End-user will contact me otherwise
}
}
private void timer1_Tick(object sender, EventArgs e)
{
try
{
// ODBC connect strings
string connectionString = "Dsn=TakeCharge";
string connString = "DSN=IDBS1;Uid=rouge;Pwd=steel";
string sqlins = "";
// Variables to be later placed inside the INSERT INTO query
string dayTime = "";
string hsmt = "";
string boft = "";
string eaft = "";
string j9st = "";
string lmbt = "";
string fbst = "";
string fcbt = "";
string fdwpt = "";
string fdept = "";
string fdbt = "";
string ffat = "";
string ffpt = "";
string fdnpt = "";
string ftdt = "";
string fppt = "";
string fgpt = "";
string hsmr = "";
string bofr = "";
string eafr = "";
string j9sr = "";
string lmbr = "";
string fbsr = "";
string fcbr = "";
string fdwpr = "";
string fdepr = "";
string fdbr = "";
string ffar = "";
string ffpr = "";
string fdnpr = "";
string ftdr = "";
string fppr = "";
string fgpr = "";
// Creates a MS Access ODBC and Oracle ODBC connection
OdbcConnection conn = new OdbcConnection(connString);
OdbcConnection DbConnection = new OdbcConnection(connectionString);
OdbcCommand DbCommand = DbConnection.CreateCommand();
DbConnection.Open();
// Checks to see that the MS Access ODBC connection is open
if (DbConnection.State == ConnectionState.Open)
{
eventLog1.WriteEntry("MS ACCESS connection is " + DbConnection.State);
DbCommand.CommandText = "SELECT Data.[Date / Time],DATA.[Hot Strip Mill Total],Data.[Basic Oxygen Furnace Total],Data.[Electro-Arc Furnace Total],Data.[J-9 Shop Total],Data.[Levy Maintence Building Total],Data.[Ford Body Shop Total], Data.[Ford Chiller Building Total],Data.[Ford Dearborn W Plant Total],Data.[Ford Dearborn E Plant Total], Data.[Ford Dearborn Balcony Total], Data.[Ford Final Assembly Total],Data.[Ford Frame Plant Total], Data.[Ford Dearborn N Plant Total],Data.[Ford Tool and Die Total], Data.[Ford Paint Plant Total],Data.[Ford Glass Plant Total], DATA.[Hot Strip Mill Rate], Data.[Basic Oxygen Furnace Rate], Data.[Electro-Arc Furnace Rate],Data.[J-9 Shop Rate],Data.[Levy Maintence Building Rate],Data.[Ford Body Shop Rate],Data.[Ford Chiller Building Rate],Data.[Ford Dearborn W Plant Rate],Data.[Ford Dearborn E Plant Rate],Data.[Ford Dearborn Balcony Rate],Data.[Ford Final Assembly Rate],Data.[Ford Frame Plant Rate],Data.[Ford Dearborn N Plant Rate],Data.[Ford Tool and Die Rate],Data.[Ford Paint Plant Rate],Data.[Ford Glass Plant Rate]FROM DATA";
// Accesses data from the MS Access ODBC connection then establishes a field counter to cycle through every column for every row
OdbcDataReader DbReader = DbCommand.ExecuteReader();
int fCount = DbReader.FieldCount;
OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);
conn.Open();
// Checks to see that the Oracle ODBC connection is open
if (conn.State == ConnectionState.Open)
{
eventLog1.WriteEntry("ORACLE connection is" + conn.State);
// Create parameters for the Oracle ODBC connection
cmdnon.Parameters.Add(dayTime, OdbcType.DateTime);
cmdnon.Parameters.Add(hsmt, OdbcType.Numeric);
cmdnon.Parameters.Add(boft, OdbcType.Numeric);
cmdnon.Parameters.Add(eaft, OdbcType.Numeric);
cmdnon.Parameters.Add(j9st, OdbcType.Numeric);
cmdnon.Parameters.Add(lmbt, OdbcType.Numeric);
cmdnon.Parameters.Add(fbst, OdbcType.Numeric);
cmdnon.Parameters.Add(fcbt, OdbcType.Numeric);
cmdnon.Parameters.Add(fdwpt, OdbcType.Numeric);
cmdnon.Parameters.Add(fdept, OdbcType.Numeric);
cmdnon.Parameters.Add(fdbt, OdbcType.Numeric);
cmdnon.Parameters.Add(ffat, OdbcType.Numeric);
cmdnon.Parameters.Add(ffpt, OdbcType.Numeric);
cmdnon.Parameters.Add(fdnpt, OdbcType.Numeric);
cmdnon.Parameters.Add(ftdt, OdbcType.Numeric);
cmdnon.Parameters.Add(fppt, OdbcType.Numeric);
cmdnon.Parameters.Add(fgpt, OdbcType.Numeric);
cmdnon.Parameters.Add(hsmr, OdbcType.Numeric);
cmdnon.Parameters.Add(bofr, OdbcType.Numeric);
cmdnon.Parameters.Add(eafr, OdbcType.Numeric);
cmdnon.Parameters.Add(j9sr, OdbcType.Numeric);
cmdnon.Parameters.Add(lmbr, OdbcType.Numeric);
cmdnon.Parameters.Add(fbsr, OdbcType.Numeric);
cmdnon.Parameters.Add(fcbr, OdbcType.Numeric);
cmdnon.Parameters.Add(fdwpr, OdbcType.Numeric);
cmdnon.Parameters.Add(fdepr, OdbcType.Numeric);
cmdnon.Parameters.Add(fdbr, OdbcType.Numeric);
cmdnon.Parameters.Add(ffar, OdbcType.Numeric);
cmdnon.Parameters.Add(ffpr, OdbcType.Numeric);
cmdnon.Parameters.Add(fdnpr, OdbcType.Numeric);
cmdnon.Parameters.Add(ftdr, OdbcType.Numeric);
cmdnon.Parameters.Add(fppr, OdbcType.Numeric);
cmdnon.Parameters.Add(fgpr, OdbcType.Numeric);
// Performs the insert query on the Oracle ODBC connection
while (DbReader.Read())
{
// Sets my variable equal to the values that are grabbed from the MS Access database table
dayTime = DbReader.GetString(0);
hsmt = DbReader["Hot Strip Mill Total"].ToString();
boft = DbReader["Basic Oxygen Furnace Total"].ToString();
eaft = DbReader["Electro-Arc Furnace Total"].ToString();
j9st = DbReader["J-9 Shop Total"].ToString();
lmbt = DbReader["Levy Maintence Building Total"].ToString();
fbst = DbReader["Ford Body Shop Total"].ToString();
fcbt = DbReader["Ford Chiller Building Total"].ToString();
fdwpt = DbReader["Ford Dearborn W Plant Total"].ToString();
fdept = DbReader["Ford Dearborn E Plant Total"].ToString();
fdbt = DbReader["Ford Dearborn Balcony Total"].ToString();
ffat = DbReader["Ford Final Assembly Total"].ToString();
ffpt = DbReader["Ford Frame Plant Total"].ToString();
fdnpt = DbReader["Ford Dearborn N Plant Total"].ToString();
ftdt = DbReader["Ford Tool and Die Total"].ToString();
fppt = DbReader["Ford Paint Plant Total"].ToString();
fgpt = DbReader["Ford Glass Plant Total"].ToString();
hsmr = DbReader["Hot Strip Mill Rate"].ToString();
bofr = DbReader["Basic Oxygen Furnace Rate"].ToString();
eafr = DbReader["Electro-Arc Furnace Rate"].ToString();
j9sr = DbReader["J-9 Shop Rate"].ToString();
lmbr = DbReader["Levy Maintence Building Rate"].ToString();
fbsr = DbReader["Ford Body Shop Rate"].ToString();
fcbr = DbReader["Ford Chiller Building Rate"].ToString();
fdwpr = DbReader["Ford Dearborn W Plant Rate"].ToString();
fdepr = DbReader["Ford Dearborn E Plant Rate"].ToString();
fdbr = DbReader["Ford Dearborn Balcony Rate"].ToString();
ffar = DbReader["Ford Final Assembly Rate"].ToString();
ffpr = DbReader["Ford Frame Plant Rate"].ToString();
fdnpr = DbReader["Ford Dearborn N Plant Rate"].ToString();
ftdr = DbReader["Ford Tool and Die Rate"].ToString();
fppr = DbReader["Ford Paint Plant Rate"].ToString();
fgpr = DbReader["Ford Glass Plant Rate"].ToString();
cmdnon.CommandText = "insert into NAT_GAS_READINGS( HSM_TOTAL,BOF_TOTAL,EAF_TOTAL,J9_SHOP_TOTAL,LEVY_TOTAL,BODY_SHOP_TOTAL,CHILLER_BLDG_TOTAL,WPLANT_TOTAL,EPLANT_TOTAL,BALCONY_TOTAL,FINAL_ASSEMBLY_TOTAL,FRAME_PLANT_TOTAL,NPLANT_TOTAL,TOOL_DIE_TOTAL,PAINT_PLANT_TOTAL,GLASS_PLANT_TOTAL,HSM,BOF,EAF,J9_SHOP,LEVY,BODY_SHOP,CHILLER_BLDG,WPLANT,EPLANT,BALCONY,FINAL_ASSEMBLY,FRAME_PLANT,NPLANT,TOOL_DIE,PAINT_PLANT,GLASS_PLANT,DATETIME) values (to_number('" + hsmt + "'), to_number('" + boft + "'), to_number('" + eaft + "'), to_number('" + j9st + "'), to_number('" + lmbt + "'),to_number('" + fbst + "'),to_number('" + fcbt + "'), to_number('" + fdwpt + "'), to_number('" + fdept + "'), to_number('" + fdbt + "'), to_number('" + ffat + "'), to_number('" + ffpt + "'), to_number('" + fdnpt + "'), to_number('" + ftdt + "'), to_number('" + fppt + "'), to_number('" + fgpt + "'), to_number('" + hsmr + "'), to_number('" + bofr + "'), to_number('" + eafr + "'), to_number('" + j9sr + "'), to_number('" + lmbr + "'), to_number('" + fbsr + "'), to_number('" + fcbr + "'), to_number('" + fdwpr + "'), to_number('" + fdepr + "'), to_number('" + fdbr + "'), to_number('" + ffar + "'), to_number('" + ffpr + "'), to_number('" + fdnpr + "'), to_number('" + ftdr + "'), to_number('" + fppr + "'), to_number('" + fgpr + "'), to_date('" + dayTime + "', 'yyyy-mm-dd HH24:MI:SS' ))";
int rowsAffected = cmdnon.ExecuteNonQuery();
}
//Closes the ODBC connections
conn.Close();
DbReader.Close();
DbCommand.Dispose();
DbConnection.Close();
}
else
{
eventLog1.WriteEntry("ORACLE connection is "+ conn.State);
}
}
else
{
eventLog1.WriteEntry("MS ACCESS connection is " + DbConnection.State);
}
}
catch (Exception ex)
{
// Sends an error to the event log, and email, the error to me regarding what is wrong.
eventLog1.WriteEntry(ex.ToString(), EventLogEntryType.Error);
EmailError(ex.ToString() +"\n"+ "(" + ex.InnerException.ToString() + ")");
}
finally
{
eventLog1.WriteEntry("Gas Meter monitoring is functioning properly.");
}
}
}
}