编写Windows命令脚本以自动执行C#代码

时间:2011-07-18 18:16:05

标签: c# automation windows-scripting

我正在编写我的第一个命令脚本作为工作项目的一部分 我编写了一个Visual Studio 2008 C#代码,它将从MS Access表中获取数据,然后将数据输入到Oracle表中。
我的问题是每分钟都在填充MS Access数据库,我需要并希望我的C#代码每隔几分钟运行一次。
因为此操作非常重要,所以此代码将在始终能够备份数据库的服务器上运行,因为它的内存中有空间。
有人告诉我,处理这个的最好方法是编写一个Windows命令脚本来处理这个问题。但是,我没有编写任何类型脚本的经验 有人可以帮我解决这个问题吗?谢谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Data.OracleClient;
using System.Data.SqlClient;
using System.IO;
using System.Data.Odbc;

namespace ConsoleApplication7
{
    class Program
    {
        static void Main(string[] args)
        {
            string connectionString = "Dsn=Gas_meter_check";

            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 = "";

            string sqlins = "";
            string connString = "DSN=Gas_Reporter_proj;Uid=rouge;Pwd=steel";
            OdbcConnection conn = new OdbcConnection(connString);

            OdbcConnection DbConnection = new OdbcConnection(connectionString);
            OdbcCommand DbCommand = DbConnection.CreateCommand();
            DbConnection.Open();
            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";         
            OdbcDataReader DbReader = DbCommand.ExecuteReader();
            int fCount = DbReader.FieldCount;
   try
  {
            OdbcCommand cmdnon = new OdbcCommand(sqlins, conn);                            
            conn.Open();

            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);


            while (DbReader.Read())
                       {
                            dayTime = DbReader.GetString(0);
                            Console.WriteLine(DbReader.GetString(0));
                            Console.WriteLine(DbReader["Date / Time"].ToString());
                            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();
                            Console.WriteLine(rowsAffected);

                        }


                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.ToString());
                    }
                    finally
                    {
                        conn.Close();                       
                    }
   DbReader.Close();
   DbCommand.Dispose();
   DbConnection.Close();
        }

    }
}

以上是编写的代码。
我的问题是,是否有人知道如何编写脚本来自动执行此代码,以便每隔几分钟运行一次。
同样,我没有编写脚本的经验。

2 个答案:

答案 0 :(得分:0)

您可以编写一个启动C#exe文件的批处理文件。

答案 1 :(得分:0)

刚刚在服务器上创建了一个计划任务,以便按特定时间间隔运行您的程序。

http://msdn.microsoft.com/en-us/library/aa383614%28v=vs.85%29.aspx

非常简单。