2013-12-23 92 views
1

我是新來的Windows開發,我想選擇一個好的數據庫管理模式,這樣我就可以用它爲我未來的應用。我發現的是這種行爲,創建本地Db->將它關聯到一個wpf DataGridView和一個DataSet->使用一個sql adapter來反映數據庫中的數據更改。這是我寫的:SqlDataAdapter的更新不能正常工作

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


    } 

有了這個代碼,我可以正確地想象我的 「Gallerie」 表裏面的數據。 ,我可以正確地從數據集刪除最後一行來與表修改的問題(我可以看到它的數據網格消失),但adp.Update()不是從真正分貝刪除同一行,所以,當我重新啓動應用程序的記錄我有刪除再次出現...沒有錯誤信息顯示。我可以怎樣從我的數據庫中刪除「真正」的記錄?

編輯

我已閱讀的地方,應SelectCommand中與適配器初始化自動創建...反正我已嘗試添加:

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

這不工作,也許我做的事情其他錯誤?

回答

0

找到了解決辦法,我的代碼是好的,行被真正刪除,並在數據集和數據庫插入...問題是另一個,使用默認設置的數據庫將覆蓋每次我建造時間和運行應用程序。通過這種方式,我看不到我用querys所做的任何更改......所以我已經將我的db文件的屬性設置爲「Never Copy」,第一次啓動應用程序時我必須手動創建的項目調試文件夾中的數據庫,但所有其他人建立數據庫響應正確的變化。

0

您需要設置SelectCommand屬性爲適配器,以便其他命令生成應該工作。請注意,這種方式會導致性能問題,這取決於應用程序中使用了多少代碼。

+0

感謝您的答覆,我已經編輯我的問題。但是,如果我多次使用該代碼,就會說這個代碼可能會導致性能問題,您能否告訴我一種適當的方法來管理像我這樣的情況? – user31929