存储过程不更新数据库

时间:2015-08-13 06:00:27

标签: stored-procedures

问题:

数据库没有更新,但我不确定它在哪里丢失。

我过去曾经使用过很多但我不明白为什么这不起作用。如果我能看到如何对存储过程运行错误测试,我怀疑这会有所帮助。

测试:

我将变量传递给标签,在.aspx代码中的存储过程请求之后检查值是否存在(在代码中留下)

将int更改为存储过程中的varchar

查询:

如何解决此问题,然后如何运行测试以便在将来找到问题

存储过程:

USE [DATABASE]
GO
/****** Object:  StoredProcedure [dbo].[spChangeValue]    Script Date: 11/08/2015 12:02:13 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
ALTER PROCEDURE  [dbo].[spRiskRatingChange] 
@ActionID int,
@EmployeeID int,
@NewRating varchar(10)

AS

DECLARE @DateChanged AS nvarchar(max) 
SET @DateChanged = GETDATE()

DECLARE @OldRating AS nvarchar(max)
DECLARE @OldComments AS nvarchar(max)
DECLARE @EmployeeName AS nvarchar(max)

SET @OldRating = 
(
SELECT OverallRiskCategory FROM TblAsbestos WHERE ID = @ActionID 
)

SET @OldComments = 
(
SELECT Comments FROM TblAsbestos WHERE ID = @ActionID 
)

SET @EmployeeName = 
(
SELECT UserFirstName + ' ' + UserSurname FROM SystemUsers  WHERE ID = @EmployeeID  
)

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
SET NOCOUNT ON;

UPDATE TblAsbestos SET OverallRiskCategory = @NewRating, RiskCategory = @NewRating,  
Comments= ('Rating changed from ' + RTRIM(@OldRating) + ' to ' 
+ RTRIM(@NewRating) + 'By ' + @EmployeeName + ' on ' + @DateChanged + ' -- ' + @OldComments) 
 WHERE ID = @ActionID

END

.aspx.vb中的代码

 Dim connection As SqlConnection
            Dim command As New SqlCommand
            Dim ds As New DataSet
            Dim ConnectionString1 As String = System.Configuration.ConfigurationManager.ConnectionStrings("VINCI_SQL").ToString()
            connection = New SqlConnection(ConnectionString1)

            connection.Open()

            With command
                .Connection = connection
                .CommandText = "spRiskRatingChange" 'include audit names
                .CommandType = CommandType.StoredProcedure
                .Parameters.Clear()
                .Parameters.AddWithValue("@ActionID", Session("ActionID").ToString)
                .Parameters.AddWithValue("@EmployeeID", Session.Item("EmployeeID").ToString)
                .Parameters.AddWithValue("@NewRating", ddOverallRiskCategoryEdit.SelectedValue)
                .ExecuteNonQuery()


                lblErrorMessageRatings.Visible = True
                lblErrorMessageRatings.Text = "Action ID: " & Session("ActionID").ToString
                lblErrorMessageRatings.Text = lblErrorMessageRatings.Text & " EmployeeID: " & Session("EmployeeID").ToString
                lblErrorMessageRatings.Text = lblErrorMessageRatings.Text & " NewRating: " & ddOverallRiskCategoryEdit.SelectedValue

阅读以下项目:

UPDATE Stored Procedure not Updating

1 个答案:

答案 0 :(得分:1)

而不是

 .Parameters.AddWithValue("@ActionID", Session("ActionID").ToString)

.Parameters.Add("@ActionId",SqlDbType.Int)
.Parameters("@ActionId").Value = CInt(Session("ActionId"))

我假设它是你传递的int值

.Parameters.AddWithValue执行隐式类型转换,也许这就是导致问题的原因