我是WPF的新手,我正在尝试让数据网格在用户编辑时自动更新本地SQL数据库中的表。
我觉得我很接近,因为我已经让插件工作了,但我无法弄清楚更新(或删除,但我还没有考虑过那么多)。
我正在为网格使用表适配器,它调用update命令的存储过程。我的问题是,当我在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
如何让update命令将ID传递给存储过程?
更新:以下更新的代码解决了我的问题
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
答案 0 :(得分:2)
此错误&#39;此行收到错误:upRiskTier需要参数@ID,这是未提供的。&#39;意思是 - 您的适配器没有正确配置..适配器中的列和参数之间没有关系.UpdateCommand ...
在下一个示例中,演示如何设置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
上的文章中看到更详细的信息