Question

I'm new to windows development, i'm trying to choose a good database management pattern, so i can use it for my future applications. What i have found is this behaviour, Create Local Db->associate it to a wpf DataGridView and a DataSet->Use of a sql Adapter to reflect the data change in the database. This is what i wrote:

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

Code Behind :

 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");


    }

With this code i can visualize correctly the data inside my "Gallerie" table. The problem come with the table modification, i can correctly delete from dataset the last row(i can see it disappearing from dataGrid), but adp.Update() not delete the same row from the real db,so when i relaunch the app the record that i have delete comes again... No error message are displayed. How i cand do to delete "really" a record from my db?

EDIT

I have read somewhere that SelectCommand should be created automatically with the Adapter init... Anyway i have tried to add:

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

This not working, maybe i'm doing something else wrong?

Était-ce utile?

La solution 2

Found the solution, my code is ok, rows are really deleted and inserted in data set and database... The problem is another, with default settings database will overwritten every time i build and run the application. In this way i can't see any change that i have made with my querys... So i have setted the property of my db file to "Never Copy", the first time i launch the app i have to made a manual copy of the db in the project Debug folder but for all the others builds the db respond correctly to changes.

Autres conseils

You need to set SelectCommand property for the adapter in order for other command generation should work. Please note that this way results in performance issues based on how much is this code used in the application.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top