2012-12-23 30 views
0

這是我的存儲過程,它執行更新,當我使用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 

通過該表執行查詢時沒有更新時和異常不拋出執行的代碼,這意味着該代碼沒有得到執行,但不能修改對數據庫所做的,我只是不知道我哪裏錯了

+0

需要定義**長度**爲任何字符的參數 - 例如使用這個:'dbcommand.Parameters.Add(「@ Comment」,SqlDbType.Char,50).Value = txtComment.Text' - 否則,你的參數是**只有一個字符長**! –

回答

2

最合乎邏輯的原因是:

InputsModule.Employee = "'Mathe BF'" 

這也許應該是:

InputsModule.Employee = "Mathe BF" 

參數會被ADO.NET被引用爲您服務。手動引用報價將導致搜索引用bf-quote,這可能不存在:)

+0

謝謝,我要檢查它是否有效 –

0

用於添加值u需要指定參數的類型,如下面的代碼所示。

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

+0

數據類型是從值中推斷出來的。對於'input'參數,通常不需要明確指定數據類型。 – Andomar

相關問題