SqlDataAdapter更新无法正常工作

时间:2013-12-23 17:32:41

标签: c# datagridview sqldataadapter

我是Windows开发的新手,我试图选择一个好的数据库管理模式,所以我可以将它用于我未来的应用程序。我发现的是这种行为,Create Local Db->将它关联到wpf DataGridView和DataSet->使用sql Adapter来反映数据库中的数据变化。这就是我写的:

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:DB" x:Class="DB.MainWindow"
    Title="MainWindow" Height="350" Width="800" Loaded="Window_Loaded">

<Window.Resources>
    <local:DataBaseMioDataSet x:Key="dataBaseMioDataSet"/>
    <CollectionViewSource x:Key="gallerieViewSource" Source="{Binding Gallerie, Source={StaticResource dataBaseMioDataSet}}"/>
</Window.Resources>

<Grid DataContext="{StaticResource gallerieViewSource}">


    <DataGrid x:Name="gallerieDataGrid" RowDetailsVisibilityMode="VisibleWhenSelected" Margin="10,10,210,109" ItemsSource="{Binding}" EnableRowVirtualization="True" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn x:Name="idColumn" Width="SizeToHeader" Header="Id" Binding="{Binding Id}"/>
            <DataGridTextColumn x:Name="idgalleriaColumn" Width="SizeToHeader" Header="idgalleria" Binding="{Binding idgalleria}"/>
            <DataGridTextColumn x:Name="pathImgColumn" Width="SizeToHeader" Header="path Img" Binding="{Binding pathImg}"/>
        </DataGrid.Columns>
    </DataGrid>
    <TextBox x:Name="idgalleriaAdd" HorizontalAlignment="Left" Height="23" Margin="612,14,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="98"/>
    <TextBox x:Name="pathgalleriaAdd" HorizontalAlignment="Left" Height="23" Margin="612,51,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="98"/>
    <Label x:Name="labell" Content="idgalleria" HorizontalAlignment="Left" Margin="724,14,0,0" VerticalAlignment="Top" />
    <Label x:Name="labell_Copy" Content="pathgalleria" HorizontalAlignment="Left" Margin="715,51,0,0" VerticalAlignment="Top" />
    <Button Content="Add" HorizontalAlignment="Left" Margin="612,96,0,0" VerticalAlignment="Top" Width="170" Click="addrow"/>
    <Button Content="Delete" HorizontalAlignment="Left" Margin="612,145,0,0" VerticalAlignment="Top" Width="170" Click="deleterow"/>

</Grid>

代码背后:

 private void Window_Loaded(object sender, RoutedEventArgs e)
    {

        DB.DataBaseMioDataSet dataBaseMioDataSet = ((DB.DataBaseMioDataSet)(this.FindResource("dataBaseMioDataSet")));
        // Carica i dati nella tabella Gallerie. Se necessario, è possibile modificare questo codice.
        DB.DataBaseMioDataSetTableAdapters.GallerieTableAdapter dataBaseMioDataSetGallerieTableAdapter = new DB.DataBaseMioDataSetTableAdapters.GallerieTableAdapter();
        dataBaseMioDataSetGallerieTableAdapter.Fill(dataBaseMioDataSet.Gallerie);
        System.Windows.Data.CollectionViewSource gallerieViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("gallerieViewSource")));
        gallerieViewSource.View.MoveCurrentToFirst();
    }


        public static SqlDataAdapter GetTableRecord(DataBaseMioDataSet datSet)
    {

        SqlConnection connection = new SqlConnection(Properties.Settings.Default.DataBaseMioStringaConnessione);//Set the connection to the SQL server
        connection.Open();


        string query = "SELECT * from Gallerie";
        SqlCommand command = new SqlCommand(query, connection);
        SqlDataAdapter adp = new SqlDataAdapter(command);
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(adp);
        adp.UpdateCommand = commandBuilder.GetUpdateCommand();
        adp.InsertCommand = commandBuilder.GetInsertCommand();
        adp.DeleteCommand = commandBuilder.GetDeleteCommand();



        return adp;
    }


 public void deleterow(object sender, RoutedEventArgs e)
    {




        DB.DataBaseMioDataSet DataBaseMioDataSet = ((DB.DataBaseMioDataSet)(this.FindResource("dataBaseMioDataSet")));

        int totalrow= DataBaseMioDataSet.Tables["Gallerie"].Rows.Count;
        int lastrow= totalrow- 1;

        DataBaseMioDataSet.Tables["Gallerie"].Rows[lastrow].Delete();

        SqlDataAdapter adp = GetTableRecord(DataBaseMioDataSet);
        adp.Fill(DataBaseMioDataSet, "Gallerie");
        adp.Update(DataBaseMioDataSet, "Gallerie");


    }

使用此代码,我可以正确地查看我的&#34; Gallerie&#34;中的数据。表。问题来自表修改,我可以正确地从数据集中删除最后一行(我可以看到它从dataGrid中消失),但是adp.Update()没有从真正的数据库中删除相同的行,所以当我重新启动应用程序时记录我已删除再来...没有显示错误信息。我该如何删除&#34;真的&#34;来自我的数据库的记录?

修改

我在某处读过应该使用Adapter init自动创建SelectCommand ...无论如何我试图添加:

string query = "SELECT * from Gallerie";
SqlCommand command = new SqlCommand(query, connection); 
adp.SelectCommand = command;

这不起作用,也许我做错了什么?

2 个答案:

答案 0 :(得分:0)

您需要为适配器设置SelectCommand属性,以便其他命令生成起作用。请注意,这种方式会导致性能问题,具体取决于应用程序中使用的代码量。

答案 1 :(得分:0)

找到解决方案,我的代码没问题,行真的被删除并插入数据集和数据库......问题是另一个问题,默认设置数据库将在每次构建和运行应用程序时被覆盖。通过这种方式,我无法看到我对我的查询所做的任何更改...所以我已将我的db文件的属性设置为“从不复制”,第一次启动应用程序时我必须手动复制项目Debug文件夹中的db,但是对于所有其他构建,db会正确响应更改。