【问题标题】:My stored procedure doesn't update when the button is clicked单击按钮时我的存储过程没有更新
【发布时间】:2012-12-23 19:49:03
【问题描述】:

这是我进行更新的存储过程,当我使用 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

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

【问题讨论】:

  • 您需要为任何字符参数定义 长度 - 例如使用这个:dbcommand.Parameters.Add("@Comment", SqlDbType.Char, 50).Value = txtComment.Text - 否则,您的参数只有一个字符长

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


【解决方案1】:

最合乎逻辑的原因是:

InputsModule.Employee = "'Mathe BF'"

应该是这样的:

InputsModule.Employee = "Mathe BF"

ADO.NET 将为您引用该参数。手动输入引号将导致搜索quote-mathe bf-quote,它可能不存在:)

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 数据类型是从值中推断出来的。对于input 参数,通常不需要显式指定数据类型。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 2016-09-15
    相关资源
    最近更新 更多