C#定期运行SQL Server程序(每1分钟)

时间:2014-01-02 10:24:01

标签: c# sql-server timer

我需要定期调用SQL Server存储过程,当我的程序运行时,它不会显示错误或异常,并且在开始时一切正常,唯一的问题是随着时间的推移,程序会突然停止,而不是做任何事。

这是我的代码。首先,我使用System.Timer让它每分钟运行

private void button2_Click(object sender, EventArgs e)
{
    System.Threading.Timer myTimer = new System.Threading.Timer(Callback, null, TimeSpan.Zero, TimeSpan.FromMinutes(1));
}

然后,在函数callback中,我调用我的SQL Server存储过程并将结果写入txt文件

public void Callback(object state)
{
   try
   {
      DataTable dt = null;
      sp_select_today_sum_ASRACD_fromMera_by_Vendor(strFrom, strTo, strCustomer, strAreaPrefix, ref dt);

      string strJJTEL_ASR = null;
      string strJJTEL_ACD = null;

      string strARCA_ASR = null;
      string strARCA_ACD = null;

      string strSHINETOWN_ASR = null;
      string strSHINETOWN_ACD = null;

      if (dt.Rows.Count > 0)
      {
         foreach (DataRow row in dt.Rows) // Loop over the rows.
         {
             if (row["vendor"].ToString() == "01.2352")      
             {
                strJJTEL_ASR = row["ASR"].ToString();
                strJJTEL_ACD = row["ACD"].ToString();
             }

             if (row["vendor"].ToString() == "01.2208")    
             {
                strARCA_ASR = row["ASR"].ToString();
                strARCA_ACD = row["ACD"].ToString();
             }

             if (row["vendor"].ToString() == "01.2238")       
             {
                strSHINETOWN_ASR = row["ASR"].ToString();
                strSHINETOWN_ACD = row["ACD"].ToString();
             }
          }
       }

       string str_report = null;

       str_report = " at " + strTo + " ,  JJtel 's ASR is " + strJJTEL_ASR + " and ACD is " + strJJTEL_ACD + "\r\n"
       + " ARCA 's ASR is " + strARCA_ASR + " and ACD is " + strARCA_ACD + "\r\n"
       + " SHINETOWN 's ASR is " + strSHINETOWN_ASR + " and ACD is " + strSHINETOWN_ACD + "\r\n";

       File.AppendAllText(strCurrentPath + @"\log.txt", str_report);
   }
   catch (Exception tep)
   {
      MessageBox.Show(tep.Message);
   }
}

这是真正调用SQL Server存储过程的函数:

public  void sp_select_today_sum_ASRACD_fromMera_by_Vendor(string timeFrom, string timeTo, string customer, string areaPrefix, ref DataTable dt)
{
    dt = new DataTable();
    SqlDataReader reader = null;
    SqlConnection conn = null;

    conn = new SqlConnection(@"Data Source=myDatasource;Initial Catalog=myDatabase;Persist Security Info=True;User ID=sa;Password=myPassword;Connect Timeout=0");
    conn.Open();

    SqlCommand sqlCmd = new SqlCommand("sp_select_today_sum_ASRACD_fromMera_by_Vendor", conn);
    sqlCmd.CommandTimeout = 120;

    try
    {
        sqlCmd.CommandType = CommandType.StoredProcedure;
        sqlCmd.Parameters.Add("@timeFrom", SqlDbType.NVarChar, 100);
        sqlCmd.Parameters["@timeFrom"].Direction = ParameterDirection.Input;
        sqlCmd.Parameters["@timeFrom"].Value = timeFrom;
        sqlCmd.Parameters.Add("@timeTo", SqlDbType.NVarChar, 100);
        sqlCmd.Parameters["@timeTo"].Direction = ParameterDirection.Input;
        sqlCmd.Parameters["@timeTo"].Value = timeTo;
        sqlCmd.Parameters.Add("@customer", SqlDbType.NVarChar, 100);
        sqlCmd.Parameters["@customer"].Direction = ParameterDirection.Input;
        sqlCmd.Parameters["@customer"].Value = customer;
        sqlCmd.Parameters.Add("@areaPrefix", SqlDbType.NVarChar, 100);
        sqlCmd.Parameters["@areaPrefix"].Direction = ParameterDirection.Input;
        sqlCmd.Parameters["@areaPrefix"].Value = areaPrefix;

        if (conn.State == ConnectionState.Closed)
           conn.Open();

        reader = sqlCmd.ExecuteReader();

        if (reader != null)
        {
           dt.Load(reader);
        }
     }
     catch (Exception ex)
     {
        File.AppendAllText(strCurrentPath + @"\log.txt", ex.Message);
     }
     finally
     {
        if (reader != null)
           reader.Dispose();

        sqlCmd.Dispose();

        if (conn != null)
        {
           conn.Close();
           conn.Dispose();
        }
     }
}

我的输出如下:

now enter key time , start query at 2014/1/2 17:27:26 with fromTime = 20140101000000 and toTime= 20140101010000
 at 20140101010000 ,  JJtel 's ASR is 0.276214 and ACD is 7.563518
 ARCA 's ASR is 0.284987 and ACD is 4.674375
 SHINETOWN 's ASR is 0.233160 and ACD is 4.081777
now enter key time , start query at 2014/1/2 17:28:13 with fromTime = 20140101010000 and toTime= 20140101020000
 at 20140101020000 ,  JJtel 's ASR is 0.097701 and ACD is 14.808235
 ARCA 's ASR is 0.303571 and ACD is 9.226666
 SHINETOWN 's ASR is 0.318181 and ACD is 10.693928
now enter key time , start query at 2014/1/2 17:29:13 with fromTime = 20140101020000 and toTime= 20140101030000
 at 20140101030000 ,  JJtel 's ASR is 0.259740 and ACD is 6.896000
 ARCA 's ASR is 0.240506 and ACD is 14.531578
 SHINETOWN 's ASR is 0.216216 and ACD is 12.030000
now enter key time , start query at 2014/1/2 17:30:14 with fromTime = 20140101030000 and toTime= 20140101040000
 at 20140101040000 ,  JJtel 's ASR is 0.150000 and ACD is 6.846666
 ARCA 's ASR is 0.243243 and ACD is 4.073333
 SHINETOWN 's ASR is 0.181818 and ACD is 0.747500
now enter key time , start query at 2014/1/2 17:31:14 with fromTime = 20140101040000 and toTime= 20140101050000
 at 20140101050000 ,  JJtel 's ASR is 0.083333 and ACD is 11.570000
 ARCA 's ASR is 0.269230 and ACD is 12.494285
 SHINETOWN 's ASR is 0.133333 and ACD is 0.670000
now enter key time , start query at 2014/1/2 17:32:21 with fromTime = 20140101050000 and toTime= 20140101060000
 at 20140101060000 ,  JJtel 's ASR is 0.157894 and ACD is 5.681666
 ARCA 's ASR is 0.375000 and ACD is 3.677333
 SHINETOWN 's ASR is 0.285714 and ACD is 5.483333
now enter key time , start query at 2014/1/2 17:33:14 with fromTime = 20140101060000 and toTime= 20140101070000
 at 20140101070000 ,  JJtel 's ASR is 0.304347 and ACD is 5.135428
 ARCA 's ASR is 0.264957 and ACD is 4.304193
 SHINETOWN 's ASR is 0.355932 and ACD is 5.980476
now enter key time , start query at 2014/1/2 17:34:14 with fromTime = 20140101070000 and toTime= 20140101080000
 at 20140101080000 ,  JJtel 's ASR is 0.280936 and ACD is 5.052500
 ARCA 's ASR is 0.308219 and ACD is 5.374777
 SHINETOWN 's ASR is 0.214285 and ACD is 2.462424
now enter key time , start query at 2014/1/2 17:35:14 with fromTime = 20140101080000 and toTime= 20140101090000
 at 20140101090000 ,  JJtel 's ASR is 0.315533 and ACD is 5.821000
 ARCA 's ASR is 0.339805 and ACD is 4.952142
 SHINETOWN 's ASR is 0.268867 and ACD is 4.965438
now enter key time , start query at 2014/1/2 17:36:16 with fromTime = 20140101090000 and toTime= 20140101100000
 at 20140101100000 ,  JJtel 's ASR is 0.326498 and ACD is 4.864444
 ARCA 's ASR is 0.346666 and ACD is 6.922115
 SHINETOWN 's ASR is 0.312500 and ACD is 8.728500

请注意最后一条记录,它显示从17:36:16开始,但从那时起,没有结果,似乎程序已关闭,但没有任何错误或异常发生,有谁能告诉我什么是原因是什么?

0 个答案:

没有答案