使用WPF数据网格更新SQL数据库表

问题描述:

我是新来的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 

这个错误 '此行得到错误: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