单击按钮时,我的存储过程不会更新

时间:2012-12-23 19:49:04

标签: sql-server vb.net visual-studio-2010

这是我执行更新的存储过程,当我使用SQL Server测试过程时,它可以很好地工作。

ALTER PROCEDURE [TKSFlex].[UpdateComment]
-- Add the parameters for the stored procedure here
  @Comment char(50),
  @Employee char(25)
AS
BEGIN TRANSACTION 
 -- SET NOCOUNT ON added to prevent extra result sets from
 -- interfering with SELECT statements.
 SET NOCOUNT On;

    -- Insert statements for procedure here

 Update whiteboard set Comment = @Comment 
 where Employee = @Employee

COMMIT

以下是单击更新按钮时执行的代码

Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal es System.EventArgs) Handles btnUpdate.Click

    InputsModule.Employee = "'Mathe BF'"
    Objconn.Open()

    dbcommand.Connection = Objconn

    dbcommand.CommandType = CommandType.StoredProcedure
    dbcommand.CommandText = "[TKSFlex].[UpdateComment]"

    dbcommand.Parameters.AddWithValue("@Comment", txtComment.Text)
    dbcommand.Parameters.AddWithValue("@Employee", InputsModule.Employee)

    'dbcommand.Parameters.Add("@Comment", SqlDbType.Char).Value = txtComment.Text
    'dbcommand.Parameters.Add("@Employee", SqlDbType.Char).Value = InputsModule.Employee
    Dim i As Integer = 0

    Try
        i = dbcommand.ExecuteNonQuery()
    Catch ex As SqlException
        MsgBox("failed")
    End Try

    Objconn.Close()
End Sub

当执行查询并且没有抛出异常时,表没有得到更新,这意味着代码确实被执行但没有对数据库进行修改,我只是不确定我在哪里出了问题

2 个答案:

答案 0 :(得分:2)

最合乎逻辑的原因是:

InputsModule.Employee = "'Mathe BF'"

应该是:

InputsModule.Employee = "Mathe BF"

ADO.NET将为您引用该参数。手动设置引号将导致搜索引号-bhe-quote,这可能不存在:)

答案 1 :(得分:0)

对于带值的add,需要指定参数的类型,如下面的代码所示。

SqlParameter parameter = command.Parameters.AddWithValue("paramName", value); parameter.SqlDbType = SqlDbType.Int;