开发者

unspecified error in C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can b开发者_如何学编程e reopened, visit the help center. Closed 11 years ago.

i wrote a program that writes data to 2003 access database every 1 minute, after 20 minutes an unspecified error occurs. Any ideas?

Here's the code

private void timer1_Tick(object sender, EventArgs e)
{
       try
        {
          OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=C:\\DATA.MDB");
          con.Open();
          new OleDbCommand("UPDATE [DATA] SET Minute = Minute+1", con).ExecuteNonQuery();
          con.Close();
        }
        catch(Exception e)
        {
            MessageBox.Show(e.Message);
        }
}


I agree with the comments that you've not provided enough information to give us much chance to diagnose the issue.

However, try moving

OleDbConnection con = new OleDbConnection(...)

Out of the timer1_Tick routine so that you're not constantly re-establishing the connection and see what happens.

We typically acquire connections and release them quickly to take advantage of connection pooling but I'm not sure you'll get connection pooling with Jet. It may be that you're effectively leaking connections.

It's just a guess, but worth considering.


OleDbConnection is a disposable resource. You must dispose it manually,

snip:

either by using using:

using (var con = new OleDbConnection(...)) {
}

or if you want to bind its lifetime to the holding object, make that holding object an IDisposable itself (You must dispose the holder manually, goto snip; )

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜