【发布时间】:2016-06-21 19:35:43
【问题描述】:
我有多个人连接到我的 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 ");
}
}
}
【问题讨论】:
-
“这不是错误”“它出错了”。当问题出现时肯定会发生一些事情,您可以分享。
-
1) 从字符串注入更改为参数,2) 捕获特定异常,而不是捕获任何异常并假设问题是“连接的用户过多”3) 处理用
using块包围你的连接和命令。 4) 完成所有这些后,发布 exact 异常,包括消息和位置,以隔离问题。 -
@cFrozenDeath 根本不是真的。 Access 可以处理许多同时连接。如果您有超过 20-30 个,那么这将开始超出 Access 的能力范围。不过两个人,应该没问题。
-
"i get an error"- 另外,您忘记了实际描述错误的部分。"i am pretty sure i shouldn't be using try catch like this"- 不,你不应该。因为您的catch块忽略实际异常。忽略错误是一种糟糕的解决方法。 -
@Brad 你说得对,我对 Access 的连接限制感到困惑