与 DataSet 和 ADO.NET 的双向数据绑定

发布于 2024-10-13 12:35:10 字数 2403 浏览 3 评论 0原文

我刚刚开始探索 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇到 2024-10-20 12:35:10

使用 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文