数据仅在表单关闭并重新打开后更新
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
数据仅在表单关闭并重新打开后更新
下载声明: 本站所有软件和资料均为软件作者提供或网友推荐发布而来,仅供学习和研究使用,不得用于任何商业用途。如本站不慎侵犯你的版权请联系我,我将及时处理,并撤下相关内容!
下载声明: 本站所有软件和资料均为软件作者提供或网友推荐发布而来,仅供学习和研究使用,不得用于任何商业用途。如本站不慎侵犯你的版权请联系我,我将及时处理,并撤下相关内容!
收藏的用户(0)
X
正在加载信息~