如何在WPF中通过tableadpater更新数据库

本文关键字:tableadpater 更新 数据库 WPF | 更新日期: 2023-09-27 17:53:16

在designeri中设置tableadapter update命令如下,在按钮事件中使用tableadpater update后,它不更新,如何在WPF中更新?

我已经添加了一个循环来手动更新数据集,并确保数据集更新与messagebox.show然而,在使用数据适配器之后。再次更新赋值给datacontext,结果没有改变,意味着数据库不更新

UPDATE        Food
SET                  Home =@Home
WHERE Name=@Name

<local:Food_DataSet x:Key="Food_DataSet" />
    </Window.Resources>
    <Grid VerticalAlignment="Top" Height="300">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition />
            <RowDefinition Height="150"/>
            <RowDefinition />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Menu Grid.Row="0" Height="20" VerticalAlignment="Top">
            <MenuItem Header="Archivo" Background="Wheat" Name="MenuItem1" >
                <MenuItem Name="mi1" Style="{StaticResource Triggers}"  Header="_Nuevo" />
                <MenuItem Name="mi2" Style="{StaticResource Triggers}"  Header="_Abrir" />
                <MenuItem Name="mi3" Style="{StaticResource Triggers}"  Header="_Salir"/>
            </MenuItem>
        </Menu>
        <ListView Grid.Row="1"
                  Name="listView"
                  Margin="5,5,5,5" 
                  ItemsSource="{Binding Mode=OneWay}" 
                  ItemContainerStyle="{DynamicResource myItemContainerStyle}"
                  ItemsPanel="{DynamicResource ItemsPanelTemplate1}" 
                  GridViewColumnHeader.Click="GridViewColumnHeaderClickedHandler">
            <ListView.View>
                <GridView>
                    <GridViewColumn DisplayMemberBinding="{Binding Name}" 
                                    Header="{Binding Path=Columns[Name], Converter={StaticResource columnHeaderConverter}}"/>
                    <GridViewColumn Header="{Binding Path=Columns[Home], Converter={StaticResource columnHeaderConverter}}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <lib:EditBox
                                    BorderThickness="0"
                                    VerticalAlignment="Stretch"
                                    HorizontalAlignment="Stretch"
                                    Value="{Binding Home, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
...
private WordProcess.Food_DataSetTableAdapters.FoodTableAdapter foodTableAdapter;
        private WordProcess.Food_DataSet FoodDataSet;
        public Window1()
        {
            InitializeComponent();
            this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            DataContext = (new FoodTableAdapter()).GetData();

            FoodDataSet = ((WordProcess.Food_DataSet)(this.FindResource("Food_DataSet")));
            foodTableAdapter = new WordProcess.Food_DataSetTableAdapters.FoodTableAdapter();
            //foodTableAdapter.Fill(FoodDataSet.Food);
        }
        private void word_button_Click(object sender, RoutedEventArgs e)
        {
                        MessageBoxResult objResult = MessageBox.Show("Are You sure You want to save", "Save Confirmation", MessageBoxButton.YesNo);
        if(objResult == MessageBoxResult.Yes)
        {
            for (int i = 0; i < FoodDataSet.Food.Count; i++)
            {
                for(int j=0; j<listView.Items.Count; j++)
                {
                    DataRowView Home = (DataRowView)listView.Items[j];
                    FoodDataSet.Food[i].Home = Home.Row[1].ToString();
                }
            }
            FoodDataSet.AcceptChanges();
            MessageBox.Show(FoodDataSet.Food[0].Home);
            try
            {
                foodTableAdapter.Update(FoodDataSet.Food);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            DataContext = foodTableAdapter.GetData();
        }

如何在WPF中通过tableadpater更新数据库

这并不奇怪。在开始你设置:FoodDataSet.AcceptChanges();当你在数据集上调用AcceptChanges时,任何仍然处于编辑模式的DataRow对象都会成功地结束它们的编辑。每个DataRow的RowState属性也会改变;添加和修改的行变为"未更改",删除的行被删除。"

然后你试试:foodTableAdapter.Update(FoodDataSet.Food);在AcceptChanges()之后,没有什么要保存到db.