我有多个人连接到我的Access数据库,如果两个人碰巧同时搜索,我会收到错误。所以我这样做是为了修复它,它减少了很多错误,但我有时也会得到它。有没有更好的办法?我很确定我不应该像这样使用try catch。
public partial class Form2 : Form
{
private OleDbConnection connection = new OleDbConnection();
public Form2()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\Documents\2015\Db12.accdb";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\Documents\2015\Db12.accdb";
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_NCHITTEST)
m.Result = (IntPtr)(HT_CAPTION);
}
private const int WM_NCHITTEST = 0x84;
private const int HT_CLIENT = 0x1;
private const int HT_CAPTION = 0x2;
private void lRead(string query, ListBox lbox)
{
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText = query;
OleDbDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
lbox.Items.Add(reader["UAS"].ToString());
}
}
catch (Exception ex)
{ MessageBox.Show("error LREAD" + ex); }
finally
{
if (reader != null)
{ reader.Dispose(); }
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
try
{
connection.Open();
listBox1.Items.Clear();
lRead($"select * from Table1 where UAS Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Customer Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Description Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Detail Like '%{textBox1.Text}%'", listBox1);
//select * from SomeTable Where SomeColumn Like '* PARMA *' dont use * use %
connection.Close();
}
catch
{
System.Threading.Thread.Sleep(500);
try
{
connection.Open();
listBox1.Items.Clear();
lRead($"select * from Table1 where UAS Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Customer Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Description Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Detail Like '%{textBox1.Text}%'", listBox1);
//select * from SomeTable Where SomeColumn Like '* PARMA *' dont use * use %
connection.Close();
}
catch
{
System.Threading.Thread.Sleep(500);
try
{
connection.Open();
listBox1.Items.Clear();
lRead($"select * from Table1 where UAS Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Customer Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Description Like '%{textBox1.Text}%'", listBox1);
lRead($"select * from Table1 where Detail Like '%{textBox1.Text}%'", listBox1);
//select * from SomeTable Where SomeColumn Like '* PARMA *' dont use * use %
connection.Close();
}
catch
{
MessageBox.Show("Error too many People Searching ");
}
}
}
答案 0 :(得分:2)
这些行没有任何用处。您分配一个新的OleDbConnection()然后让它超出范围。连接是有限的资源,没有你将不会使用的连接。
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\Documents\2015\Db12.accdb";
行
private OleDbConnection connection = new OleDbConnection();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=S:\Documents\2015\Db12.accdb";
应该将移动到实际访问数据库的方法中。由于连接是有限的资源,因此它们实现IDisposable。任何实现IDisposable的东西都应该使用using statement来确保尽快释放相关资源。您可能会限制并发性,因为您在此处没有这样做。相反,您可以保持表单生命周期内的连接,以及垃圾收集器清理表单所需的时间。
在textBox1_TextChanged
中,您会多次重复相同的代码。这很容易出错(如果您在一个地方更改代码但忘记在其他地方进行更改,该怎么办)。如果需要这种重试逻辑,请使用循环。此外,我建议您稍微随机化一下您的睡眠时间,以便多次获得错误的用户在重试之前完全睡眠的时间完全相同。将连接分配也移动到此方法中,以便在最短的时间内保持连接。
迄今为止的评论也很有用。我不会在这里重复这个建议。