在我的C#应用程序中,我有一个目录列表,我希望用户能够在网格上显示和更新。
为了完成这项工作,我显示了网格并设置了更新命令,因此每当用户更改网格中的内容时,MsAccess数据库都会更新。
每当一个&#34;有些东西编目&#34;按下按钮,运行UpdateGrid函数。此函数接收表的名称和更新查询,以及用于进行更新的参数列表。private void UpdateGrid(string data, string queryUpdate, List<Tuple<string, OleDbType,int>> parameters, bool Fuse, string description)
{
tblName.Text = description;
try
{
OleDbConnection conn = (Fuse ? new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Fusing.mdb") : new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=MaterialDB.mdb"));
OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + data, conn);
myDataSet = new DataSet();
adapter.Fill(myDataSet, data);
DataTable myDataTable = myDataSet.Tables[data];
// Create the UpdateCommand.
OleDbCommand command = new OleDbCommand(queryUpdate, conn);
// Add the parameters for the UpdateCommand.
foreach (var param in parameters)
{
command.Parameters.Add("@" + param.Item1, param.Item2, param.Item3, param.Item1);
}
command.Parameters.Add("@ID", OleDbType.Integer, 5, "ID");
adapter.UpdateCommand = command;
// Update the DB whenever a row changes
myDataTable.RowChanged += (s, f) =>
{
adapter.Update(myDataTable);
};
var be = CatalogsGrid.GetBindingExpression(DataGrid.ItemsSourceProperty);
CatalogsGrid.DataContext = myDataSet;
CatalogsGrid.ItemsSource = myDataSet.Tables[0].DefaultView;
Binding nbe = new Binding
{
Source = myDataSet,
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Path = new PropertyPath(data),
BindsDirectlyToSource = true
};
CatalogsGrid.SetBinding(DataGrid.ItemsSourceProperty, nbe);
CatalogsGrid.Items.Refresh();
}
catch (Exception)
{
return;
}
}
此功能按预期工作。
但是,我尝试遵循Disposing指南,并在每个数据库元素中放置using
。当我这样做,一切都变成了:
using(OleDbConnection conn = (Fuse ? new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Fusing.mdb") : new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=MaterialDB.mdb")))
{ }
然后更新停止使用一般错误,告诉您Update查询错误。
我应该在每个Connetion
,DataAdapter
和Command
周围使用,或者只要C#决定让它们死掉。
编辑:
使用的代码是下一个:
private void UpdateGrid(string data, string queryUpdate, List<Tuple<string, OleDbType,int>> parameters, bool Fuse, string description)
{
tblName.Text = description;
try
{
using (OleDbConnection conn = (Fuse ? new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Fusing.mdb") : new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=MaterialDB.mdb")))
{
using (OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + data, conn))
{
myDataSet = new DataSet();
adapter.Fill(myDataSet, data);
DataTable myDataTable = myDataSet.Tables[data];
// Create the UpdateCommand.
using (OleDbCommand command = new OleDbCommand(queryUpdate, conn))
{
// Add the parameters for the UpdateCommand.
foreach (var param in parameters)
{
command.Parameters.Add("@" + param.Item1, param.Item2, param.Item3, param.Item1);
}
command.Parameters.Add("@ID", OleDbType.Integer, 5, "ID");
adapter.UpdateCommand = command;
// Update the DB whenever a row changes
myDataTable.RowChanged += (s, f) =>
{
adapter.Update(myDataTable);
};
var be = CatalogsGrid.GetBindingExpression(DataGrid.ItemsSourceProperty);
CatalogsGrid.DataContext = myDataSet;
CatalogsGrid.ItemsSource = myDataSet.Tables[0].DefaultView;
Binding nbe = new Binding
{
Source = myDataSet,
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Path = new PropertyPath(data),
BindsDirectlyToSource = true
};
CatalogsGrid.SetBinding(DataGrid.ItemsSourceProperty, nbe);
CatalogsGrid.Items.Refresh();
}
}
}
}
catch (Exception)
{
return;
}
}
运行此代码时,我收到以下异常。
我在互联网上的搜索说这是数据库更新过程中发生的任何错误的一般错误,99%的时间不是查询中的错误(我们知道情况并非如此,因为在我们把使用声明放到这个地方之前,一切都运转正常。)