8wDlpd.png
8wDFp9.png
8wDEOx.png
8wDMfH.png
8wDKte.png

数据仅在表单关闭并重新打开后更新

Jimoh Tajudeen 1月前

16 0

我有一个从 slq express 检索数据的表单。如果我更改任何字段并保存数据,它会立即恢复到更改之前的数据。但是,如果我关闭表单并重新打开...

我有一个从 slq express 检索数据的表单。如果我更改任何字段并保存数据,它会立即恢复到更改之前的数据。但是,如果我关闭表单并重新打开它,则会显示更新的数据。我已经尝试了所有能想到的方法来解决这个问题,有人能告诉我我做错了什么吗?如果我没有解释清楚,请原谅。代码如下。

Public Class FrmStaffDetails
    Dim Con As New SqlConnection("Data Source=LAPTOP\SQLEXPRESS;Initial Catalog=SimpleMaintenanceSystem;Integrated Security=True;Encrypt=True;TrustServerCertificate=True")
    Dim cmd As New SqlCommand
    Dim Index As Integer = 0
    Dim table As New DataTable()
    Dim i As Integer
    Dim UserStatus As String

    Private Sub FrmStaffDetails_Load(sender As Object, e As EventArgs) Handles MyBase.Load

        Dim Con As New SqlConnection("Data Source=LAPTOP\SQLEXPRESS;Initial Catalog=SimpleMaintenanceSystem;Integrated Security=True;Encrypt=True;TrustServerCertificate=True")

        If Con.State = ConnectionState.Open Then
            Con.Close()
        End If
        Con.Open()

        'Disp_Data()
        ShowData(Index)

    End Sub

    Private Sub ShowData(Position As Integer)

        Dim Cmd As New SqlCommand("Select * From Staff", Con)
        Dim adaptor As New SqlDataAdapter(Cmd)
        adaptor.Fill(table)

        If Con.State = ConnectionState.Open Then
                Con.Close()
            End If
            Con.Open()

            TxtStaffID.Text = table.Rows(Position)(0).ToString()
            TxtStaffFirstName.Text = table.Rows(Position)(1).ToString()
            TxtStaffLastName.Text = table.Rows(Position)(2).ToString()
            TxtStaffAddress.Text = table.Rows(Position)(3).ToString()
            TxtStaffCity.Text = table.Rows(Position)(4).ToString()
            TxtStaffState.Text = table.Rows(Position)(5).ToString()
            TxtStaffPostCode.Text = table.Rows(Position)(6).ToString()
            TxtStaffPhoneNumber.Text = table.Rows(Position)(7).ToString()
            TxtStaffEmail.Text = table.Rows(Position)(8).ToString()
            ChkActiveUser.Checked = table.Rows(Position)(9).ToString()
            ChkOpsUser.Checked = table.Rows(Position)(10).ToString()
            RBAdminUser.Checked = table.Rows(Position)(11).ToString()
            RBStdUser.Checked = table.Rows(Position)(12).ToString()
            RBReadOnlyUser.Checked = table.Rows(Position)(13).ToString()
            TxtStaffUserName.Text = table.Rows(Position)(14).ToString()
            TxtStaffPassword.Text = table.Rows(Position)(15).ToString()
            RTBStaffNotes.Text = table.Rows(Position)(16).ToString()
            TxtLaborCostStd.Text = table.Rows(Position)(17).ToString()
            TxtLaborCostTimeHalf.Text = table.Rows(Position)(18).ToString()
            TxtLaborCostDouble.Text = table.Rows(Position)(19).ToString()
            TxtLaborSellStd.Text = table.Rows(Position)(20).ToString()
            TxtLaborSellTimeHalf.Text = table.Rows(Position)(21).ToString()
            TxtLaborSellDouble.Text = table.Rows(Position)(22).ToString()
            ChkChargableUser.Checked = table.Rows(Position)(36).ToString()
            TxtStaffEmergencyContactName.Text = table.Rows(Position)(37).ToString()
            TxtStaffEmergencyPhoneNumber.Text = table.Rows(Position)(38).ToString()
            TxtStaffEmergencyEmail.Text = table.Rows(Position)(39).ToString()
            TxtStaffHomeEmail.Text = table.Rows(Position)(40).ToString()

    End Sub

    Private Sub BtnUpdateData_Click(sender As Object, e As EventArgs) Handles BtnUpdateData.Click
        If Con.State = ConnectionState.Open Then
            Con.Close()
        End If
        Con.Open()

        cmd = Con.CreateCommand()
        cmd.CommandType = CommandType.Text

        cmd.CommandText = "UPDATE Staff SET StaffFirstname = @FirstName
        , StaffLastName = @LastName
        , StaffAddress = @Address
        , StaffCity = @city
        , StaffState = @State
        , StaffPostCode = @PostCode
        , StaffPhoneNumber = @PhoneNumber
        , StaffActive = @Active
        , StaffOps = @OpsUser
        , StaffUserName = @UserName
        , StaffPassword = @Password
        , StaffAdminUser = @AdminUser
        , StaffStdUser = @STDUser
        , StaffReadOnlyUser = @ReadOnlyUser
        , StaffNotes = @Notes
        , Chargable = @Charge
        , LaborCostSTD = @CostSTD
        , LaborCostTimeHalf = @CostTimeHalf
        , LaborCostDouble = @CostDouble
        , LaborSellSTD = @SellSTD
        , LaborSellTimeHalf = @SellTimeHalf
        , LaborSellDouble = @SellDouble
        , StaffEmergencyName = @EMName
        , StaffEmergencyPhone = @EMPhone
        , StaffEmergencyEmail = @EMEmail
        , StaffHomeEmail = @HomeEmail WHERE Staffid = @StaffId"

        cmd.Parameters.AddWithValue("@staffID", TxtStaffID.Text)
        cmd.Parameters.AddWithValue("@FirstName", TxtStaffFirstName.Text)
        cmd.Parameters.AddWithValue("@LastName", TxtStaffLastName.Text)
        cmd.Parameters.AddWithValue("@address", TxtStaffAddress.Text)
        cmd.Parameters.AddWithValue("@city", TxtStaffCity.Text)
        cmd.Parameters.AddWithValue("@State", TxtStaffState.Text)
        cmd.Parameters.AddWithValue("@Postcode", TxtStaffPostCode.Text)
        cmd.Parameters.AddWithValue("@PhoneNumber", TxtStaffPhoneNumber.Text)
        cmd.Parameters.AddWithValue("@Active", SqlDbType.Bit).Value = ChkActiveUser.Checked
        cmd.Parameters.AddWithValue("@OpsUser", SqlDbType.Bit).Value = ChkOpsUser.Checked
        cmd.Parameters.AddWithValue("@UserName", TxtStaffUserName.Text)
        cmd.Parameters.AddWithValue("@Password", TxtStaffPassword.Text)
        cmd.Parameters.AddWithValue("@AdminUser", SqlDbType.Bit).Value = RBAdminUser.Checked
        cmd.Parameters.AddWithValue("@STDUser", SqlDbType.Bit).Value = RBStdUser.Checked
        cmd.Parameters.AddWithValue("@ReadOnlyUser", SqlDbType.Bit).Value = RBReadOnlyUser.Checked
        cmd.Parameters.AddWithValue("@Notes", RTBStaffNotes.Text)
        cmd.Parameters.AddWithValue("@Charge", SqlDbType.Bit).Value = ChkChargableUser.Checked
        cmd.Parameters.AddWithValue("@CostSTD", TxtLaborCostStd.Text)
        cmd.Parameters.AddWithValue("@CostTimeHalf", TxtLaborCostTimeHalf.Text)
        cmd.Parameters.AddWithValue("@CostDouble", TxtLaborCostDouble.Text)
        cmd.Parameters.AddWithValue("@SellSTD", TxtLaborSellStd.Text)
        cmd.Parameters.AddWithValue("@SellTimeHalf", TxtLaborSellTimeHalf.Text)
        cmd.Parameters.AddWithValue("@SellDouble", TxtLaborSellDouble.Text)
        cmd.Parameters.AddWithValue("@EMName", TxtStaffEmergencyContactName.Text)
        cmd.Parameters.AddWithValue("@EMPhone", TxtStaffEmergencyPhoneNumber.Text)
        cmd.Parameters.AddWithValue("@EMEmail", TxtStaffHomeEmail.Text)
        cmd.Parameters.AddWithValue("@HomeEmail", TxtStaffHomeEmail.Text)

        cmd.ExecuteNonQuery()
        MessageBox.Show("Data Inserted Succesfully")

        con.close()

        ShowData(Index)

    End Sub

End Class
帖子版权声明 1、本帖标题:数据仅在表单关闭并重新打开后更新
    本站网址:http://xjnalaquan.com/
2、本网站的资源部分来源于网络,如有侵权,请联系站长进行删除处理。
3、会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。
4、本站一律禁止以任何方式发布或转载任何违法的相关信息,访客发现请向站长举报
5、站长邮箱:yeweds@126.com 除非注明,本帖由Jimoh Tajudeen在本站《vb.net》版块原创发布, 转载请注明出处!
最新回复 (0)
返回
作者最近主题: