2017-08-08 70 views
0

开放时,我调用这个函数在计时器的都跟我说 的连接并没有关闭连接的当前状态是开放的连接并没有关闭连接的当前状态为C#

任何帮助,请...

public static void Notify(string source, string query, ref OleDbConnection connection) 
    { 
     OleDbCommand command = new OleDbCommand(query, connection); 
     try 
     { 
      connection.Open(); 
      OleDbDataReader reader = command.ExecuteReader(); 

      while (reader.Read()) 
      { 
       DateTime date = DateTime.Parse(reader[0].ToString()); 
       if (date.ToShortDateString() == DateTime.Now.ToShortDateString()) 
       { 
        DateTime time = DateTime.Parse(reader[1].ToString()); 
        if (time.ToShortTimeString() ==DateTime.Now.ToShortTimeString()) 
        { 
         string notification = source + " You have " + reader[2].ToString() + " at " + (time.ToLongTimeString()).ToString(); 

         MessageBox.Show(notification, "Reminder", MessageBoxButtons.OK, MessageBoxIcon.Information); 

        } 
       } 
      } 

     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
     finally 
     { 
      connection.Close(); 
     } 
    } 
+0

在哪一行? – tchelidze

+2

您是否在其他地方使用连接?你提供它作为参数,所以也许它不是唯一一次使用它? – Karl

+0

有没有错误的语法,它只是当时间是正确的(等于时间.now) 其重复的消息为60秒多, 它告诉我,我会停止时间不正确 –

回答

2

正如消息所示,您的连接已经打开。这意味着您已拨打

connection.Open(); 

某个地方在致电“通知”之前。检查你的代码,看看是否是这种情况。希望能帮助到你。

+0

这个函数,这个函数执行一次,并且它去异常 –

+0

尝试调试你的代码。将断点设置到connection.Open()被调用并查看会发生什么的行。如果系统在到达此线路时显示错误,则意味着连接已经打开,正如我上面提到的那样。如果你需要更多的信息,请告诉我 –

相关问题