2017-06-05 91 views
1

我是新來的WPF,我試圖讓一個DataGrid在用戶編輯它時自動更新本地SQL數據庫中的一個表。使用WPF數據網格更新SQL數據庫表

我覺得我已經接近插入的工作,但我無法弄清楚更新(或刪除,但我還沒有看過那麼多)。

我正在使用表格適配器調用更新命令的存儲過程。我的問題是,當我在CurrentCellChanged事件中爲它調用update命令時,它沒有傳入存儲過程的ID參數,所以更新失敗。

這裏是背後的WPF xaml.vb頁面的相關代碼:

Dim oConn As New SqlConnection(ConfigurationManager.ConnectionStrings("AARP_Conn").ToString) 
' construct the dataset 
Dim dataset As New AARPDataSet 
' use a table adapter to populate the Customers table 
Dim adapter As New AARPDataSetTableAdapters.tblRiskTiersTableAdapter 

Private Sub _grdRiskTiers_CurrentCellChanged(sender As Object, e As EventArgs) Handles _grdRiskTiers.CurrentCellChanged 
    'this line gets error: upRiskTier expects parameter @ID, which was not supplied. 
    adapter.Update(dataset.tblRiskTiers) 
End Sub 


Private Sub RiskTiersForm_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized 

     adapter.Fill(dataset.tblRiskTiers) 

     ' use the dataset as the DataContext for this Window 
     Me.DataContext = dataset.tblRiskTiers.DefaultView 

End Sub 

這裏是DataGrid的標記:

<DataGrid x:Name="_grdRiskTiers" AutoGenerateColumns="False" ItemsSource="{Binding}" HorizontalAlignment="Left" Margin="45,20,0,0" VerticalAlignment="Top" Height="199" Width="192" Grid.ColumnSpan="2"> 
      <DataGrid.Columns> 
       <DataGridTextColumn Binding="{Binding ID}" ></DataGridTextColumn> 
       <DataGridTextColumn Binding="{Binding LowTierCreditScore}" Header="Low Tier Credit Score"></DataGridTextColumn> 
      </DataGrid.Columns> 
</DataGrid> 

,這裏是用於更新存儲過程:

CREATE PROCEDURE [dbo].[upRiskTier] 
    @ID as int, 
    @NewLowTierCreditScore as int 
AS 
IF EXISTS(SELECT * FROM tblRiskTiers WHERE LowTierCreditScore = @NewLowTierCreditScore) BEGIN 
    DELETE FROM tblRiskTiers WHERE ID = @ID 
END ELSE BEGIN 
    UPDATE tblRiskTiers SET LowTierCreditScore = @NewLowTierCreditScore 
    WHERE ID = @ID 
END 

如何獲取更新命令以將ID傳遞到存儲過程?

UPDATE:下面的更新的代碼解決了我的問題

Private Sub RiskTiersForm_Initialized(sender As Object, e As EventArgs) Handles Me.Initialized 

adapter.Fill(dataset.tblRiskTiers) 

Dim command = New SqlCommand("[dbo].[upRiskTier]") 
command.CommandType = System.Data.CommandType.StoredProcedure 
command.Connection = oConn 
command.Parameters.Add("@ID", System.Data.SqlDbType.Int, 5, "ID") 
command.Parameters.Add("@NewLowTierCreditScore", System.Data.SqlDbType.Int, 5, "LowTierCreditScore") 
adapter.Adapter.UpdateCommand = command 
' use the Customer table as the DataContext for this Window 
Me.DataContext = dataset.tblRiskTiers.DefaultView 

End Sub 

回答

2

這個錯誤 '此行得到錯誤:upRiskTier預計參數@ID,但未提供。'意思是說 - 你的適配器沒有正確配置..你的適配器中的列和參數之間沒有關係。更新命令...

下一個例子顯示瞭如何設置DataTable中的列和命令參數之間的關係。檢查你的代碼在哪裏配置SqlDataAdapter。

// Create the UpdateCommand. 
    command = new SqlCommand(
     "UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " + 
     "WHERE CustomerID = @oldCustomerID", connection); 

    // Add the parameters for the UpdateCommand. 
    command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID"); 
    command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"); 
    SqlParameter parameter = command.Parameters.Add(
     "@oldCustomerID", SqlDbType.NChar, 5, "CustomerID"); 
    parameter.SourceVersion = DataRowVersion.Original; 

    adapter.UpdateCommand = command; 

在你的情況 - 你的命令應該是

command = new SqlCommand("[dbo].[upRiskTier]"); 
command.CommandType = CommandType.StoredProcedure; 

更詳細的您可以在文章上看到MSDN

+0

我真的不明白,爲什麼這個工作,因爲我已經設置參數我的數據集中的列和插入已經以這種方式工作,但是像這樣在後面的代碼中創建更新命令確實解決了這個問題。 – rgorr