SQL Server数据库更新失败



这是我的代码

Imports System.Data.SqlClient
Public Class pinchange
    Private Sub btn_enter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_enter.Click
        Using _conn As New SqlConnection("Data Source=HOME-PC; Initial Catalog=csbanking; Integrated Security= true")
            Using _comm As New SqlCommand()
                With _comm
                    .Connection = _conn
                    .CommandText = "UPDATE USERLOGIN SET USER_PIN = @pin WHERE USER_LOGIN = @id"
                    .CommandType = CommandType.Text
                    .Parameters.AddWithValue("@pin", txt_newpin.Text)
                    .Parameters.AddWithValue("@id", Login.txt_id.Text)
                End With
                Try
                    _conn.Open()
                    _comm.ExecuteNonQuery()
                Catch ex As SqlException
                    MsgBox(ex.Message.ToString())
                End Try
            End Using
        End Using
        MessageBox.Show("Pin Change Successful")
        _1stpage.Show()
        Login.txt_id.Text = ""
        Security.msktxt_pin.Text = ""
        Me.Hide()
    End Sub
End Class

代码成功,但未能更新Pin Change后,我添加了"Where USER_LOGIN = @id"怎么了?

Try This

    Using _conn As New SqlConnection("Data Source=HOME-PC; Initial Catalog=csbanking; Integrated Security= true")
        Using _comm As New SqlCommand()
            With _comm
                .Connection = _conn
                .CommandText = "UPDATE USERLOGIN SET USER_PIN = @pin WHERE USER_LOGIN = @id"
                .CommandType = CommandType.Text
                .Parameters.Add("@pin", SqlDbType.NVarChar).Value = txt_newpin.Text
                .Parameters.Add("@id", SqlDbType.NVarChar).Value = txt_id.Text
            End With
            Try
                _conn.Open()
                _comm.ExecuteNonQuery()
            Catch ex As SqlException
                MsgBox(ex.Message.ToString())
            End Try
        End Using
    End Using
    MessageBox.Show("Pin Change Successful")
    _1stpage.Show()
    Login.txt_id.Text = ""
    Security.msktxt_pin.Text = ""
    Me.Hide()

我已经解决了我的问题

在我的登录表单公共共享id为string = "然后Id = txt_id.text

在我的pin change form中以前的.CommandText = "UPDATE USERLOGIN SET USER_PIN = @pin WHERE USER_LOGIN = @id "然后.Parameters。AddWithValue("@ id",Login.txt_id.Text)

解决.CommandText = "UPDATE USERLOGIN SET USER_PIN = @pin WHERE USER_LOGIN = @id "然后.Parameters。AddWithValue("@ id",Login.id)

感谢所有关心和帮助我的人谢谢你的帮助,非常感谢你的好意和帮助

到目前为止一切似乎都很好。

如果你能展示你的表模式就太好了。

可能传递的参数可能有拼写问题或其他问题。

同时,尝试使用存储过程方法。

Create procedure dbo.[Usp_UserPin_Update]
@UserPin varchar(20) ,
@UserID int
AS
BEGIN
     UPDATE dbo.[UserLogin]
     SET User_Pin = @UserPin
     WHERE User_Id = @UserID
END

应该没问题。

希望有帮助!

相关内容

  • 没有找到相关文章

最新更新