unspecified error in C# [closed]
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;
)
精彩评论