与 DataSet 和 ADO.NET 的双向数据绑定
我刚刚开始探索 WPF 及其在数据绑定方面的可能性。
我有一个示例 Access 数据库文件并将其内容放入 DataSet
中。然后我将 DataSet
绑定到网格(双向绑定)。我想要实现的是更新已更改的 DataSet
-Item 的底层数据库条目。这是我的代码:
public partial class MainWindow : Window
{
DataSet sampleDataSet;
OleDbDataAdapter adapter;
public MainWindow()
{
InitializeComponent();
InitializeDB();
}
private void InitializeDB()
{
string mdbFile = "./test.mdb";
string connString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile);
OleDbConnection conn = new OleDbConnection(connString);
adapter = new OleDbDataAdapter("SELECT * FROM Player;", conn);
// ----------------------
// **EDIT** inserted update command
OleDbCommand cmd = new OleDbCommand("UPDATE Player SET Name = @Name " +
"WHERE ID = @ID", conn);
cmd.Parameters.Add("@Name", OleDbType.VarChar, 40, "Name");
cmd.Parameters.Add("@ID", OleDbType.Char, 5, "ID");
// set the update command
adapter.UpdateCommand = cmd;
// **End of EDIT**
// ----------------------
sampleDataSet = new DataSet("Player Table");
adapter.Fill(sampleDataSet, "Player");
data1.DataContext = sampleDataSet;
}
private void data1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
adapter.Update(sampleDataSet, "Player");
}
}
// Edit Comment April, 14th - Begin
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//sampleDataSet.AcceptChanges();
adapter.Update(sampleDataSet, "Player"); // Calling update again fixes it, but why?
}
// Edit Comment April, 14th - End
}
我的 XAML 如下所示:
<Window x:Class="AdoTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="data1" AutoGenerateColumns="True" ItemsSource="{Binding Mode=TwoWay, Path=Player}" CellEditEnding="data1_CellEditEnding" />
</Grid>
将网格中更改的值实际保存到数据库文件的方法是什么?
I'm just beginning to explore WPF and its possibilities regarding data binding.
I've got a sample Access database file and put its content into a DataSet
. Then I bind the DataSet
to a Grid (two way binding). What I want to achieve is to update the underlying database entry of the changed DataSet
-Item. This is my code:
public partial class MainWindow : Window
{
DataSet sampleDataSet;
OleDbDataAdapter adapter;
public MainWindow()
{
InitializeComponent();
InitializeDB();
}
private void InitializeDB()
{
string mdbFile = "./test.mdb";
string connString = string.Format(
"Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}", mdbFile);
OleDbConnection conn = new OleDbConnection(connString);
adapter = new OleDbDataAdapter("SELECT * FROM Player;", conn);
// ----------------------
// **EDIT** inserted update command
OleDbCommand cmd = new OleDbCommand("UPDATE Player SET Name = @Name " +
"WHERE ID = @ID", conn);
cmd.Parameters.Add("@Name", OleDbType.VarChar, 40, "Name");
cmd.Parameters.Add("@ID", OleDbType.Char, 5, "ID");
// set the update command
adapter.UpdateCommand = cmd;
// **End of EDIT**
// ----------------------
sampleDataSet = new DataSet("Player Table");
adapter.Fill(sampleDataSet, "Player");
data1.DataContext = sampleDataSet;
}
private void data1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
if (e.EditAction == DataGridEditAction.Commit)
{
adapter.Update(sampleDataSet, "Player");
}
}
// Edit Comment April, 14th - Begin
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
//sampleDataSet.AcceptChanges();
adapter.Update(sampleDataSet, "Player"); // Calling update again fixes it, but why?
}
// Edit Comment April, 14th - End
}
My XAML looks like this:
<Window x:Class="AdoTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Name="data1" AutoGenerateColumns="True" ItemsSource="{Binding Mode=TwoWay, Path=Player}" CellEditEnding="data1_CellEditEnding" />
</Grid>
What is the approach to actually persist the changed values in the grid to the database file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 DataAdapter 的 Update 方法。这将要求您使用执行实际插入、更新和删除的命令来设置 InsertMethod、UpdateMethod 和 DeleteMethod 属性。您还可以使用 System.Data.OleDb.OleDbCommandBuilder< /a> 只要选择命中单个表(包括其主键或至少一个唯一列),就会为您生成查询
也请参阅此内容
另一种方法是使用实体框架并拥有它为您生成查询。
编辑再想一想,你的问题是什么?您已经在执行此操作(设置更新、插入和删除命令除外。)
看看这里查看如何将数据库与数据集同步
Use the Update Method of the DataAdapter. This will require you to set the InsertMethod, UpdateMethod and DeleteMethod properties with commands that do the actual inserts, updates and deletes. You could also use the System.Data.OleDb.OleDbCommandBuilder to generate the queries for you as long as the select hits a single table (including its primary key or at least one unique column)
See this too
Another way woul dbe to use the entity framework and have that generate the queries for you.
EDIT on second thought, what is your question? You are already doing this (except for setting the update, insert and delete commands.)
Look here to see how to sync the database with the Dataset