Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# SqlDataAdapter更新不工作_C#_Datagridview_Sqldataadapter - Fatal编程技术网

C# SqlDataAdapter更新不工作

C# SqlDataAdapter更新不工作,c#,datagridview,sqldataadapter,C#,Datagridview,Sqldataadapter,我是windows开发的新手,我正在尝试选择一个好的数据库管理模式,这样我就可以在未来的应用程序中使用它。我发现的是这种行为,创建本地数据库->将其与wpf DataGridView和DataSet关联->使用sql适配器来反映数据库中的数据更改。这是我写的: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.

我是windows开发的新手,我正在尝试选择一个好的数据库管理模式,这样我就可以在未来的应用程序中使用它。我发现的是这种行为,创建本地数据库->将其与wpf DataGridView和DataSet关联->使用sql适配器来反映数据库中的数据更改。这是我写的:

<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>
使用此代码,我可以正确地可视化“Gallerie”表中的数据。表修改带来的问题是,我可以正确地从数据集中删除最后一行(我可以看到它从dataGrid中消失),但adp.Update()不会从实际数据库中删除同一行,因此当我重新启动应用程序时,我删除的记录会再次出现。。。不显示任何错误消息。如何从数据库中删除“真的”记录

编辑

我在某个地方读到,SelectCommand应该使用AdapterInit自动创建。。。无论如何,我试图补充:

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

这不起作用,可能是我做错了什么?

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

找到了解决方案,我的代码正常,行被删除并插入到数据集和数据库中。。。问题是另一个,默认设置会在每次构建和运行应用程序时覆盖数据库。这样的话,我就看不到我对我的查询所做的任何改变。。。因此,我已将db文件的属性设置为“从不复制”,第一次启动应用程序时,我必须在project Debug文件夹中手动复制db,但对于所有其他版本,db会正确响应更改。

感谢您的回复,我已编辑了我的问题。但是,您说如果我多次使用此代码,可能会导致性能问题,您能告诉我一种正确的方法来管理类似于我的情况吗?
string query = "SELECT * from Gallerie";
SqlCommand command = new SqlCommand(query, connection); 
adp.SelectCommand = command;