【问题标题】:How to create an Update button for a database?如何为数据库创建更新按钮?
【发布时间】:2016-11-01 03:28:54
【问题描述】:

我的更新按钮有问题,我不知道从哪里开始更新我的 SQL 数据库,我使用 SSMS Microsoft 创建我的表主题,我还将它与我拖动的 VS 2010 ULTIMATE 连接我的表到 LINQtoSQL 和 DATASET,我创建了一个保存按钮,该按钮成功保存到我的数据库并刷新了我的 datagridview。

[ Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    If txtSubjectName.Text = "" Then
        ErrorProvider1.SetError(txtSubjectName, "Subject Name Cannot be Empty!")
        Exit Sub
    ElseIf txtSubSName.Text = "" Then
        ErrorProvider2.SetError(txtSubSName, "Subject Short Name Cannot be Empty!")
        Exit Sub
    End If
    Dim db As New EMSDataContext
    Dim SaveSubject = From C In db.Subjects
            Where C.SubjectName = txtSubjectName.Text
            Select C

    If SaveSubject.Count <> 0 Then
        MsgBox("Subject already exits!!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Alart!!")
        Exit Sub
    Else
        Dim SaveNSubject As New Subject With {.SubjectName = txtSubjectName.Text, .ShortName = txtSubSName.Text}
        db.Subjects.InsertOnSubmit(SaveNSubject)
        db.SubmitChanges()
        MsgBox("Subject added successfully!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Information")
    End If
    Registration_Load(sender, e)
    txtSubjectName.Text = ""
    txtSubSName.Text = ""
End Sub]

我还有一个删除按钮,它成功地从数据库中删除了我的数据并刷新了我的 datagridview。

  [Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
    Dim A As New EMSDataContext
    Dim B = From C In A.Subjects
            Where C.ID = Val(DtgvSubject.CurrentRow.Cells(0).Value)
            Select C
    Try
        A.Subjects.DeleteOnSubmit(B.FirstOrDefault)
        If MsgBox("Are You Sure to Delete This Record?", MsgBoxStyle.Question + vbYesNo, "Question") = MsgBoxResult.Yes Then
            A.SubmitChanges()
            MsgBox("Record Deleted Successfully!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Information")
            txtSubjectName.Text = ""
            txtSubSName.Text = ""
            Registration_Load(sender, e)
        Else
            txtSubjectName.Text = ""
            txtSubSName.Text = ""
            Registration_Load(sender, e)
        End If
    Catch ex As Exception
        MsgBox("Select a Record First!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "ALERT!!!")
    End Try
    If B.Count = 0 Then
        MsgBox("Please select list to delete!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Alart!!")

    End If

End Sub]

我还创建了 datagridview 单元格的双击事件,如果我双击这也是成功的,我会在其中获取单元格值到我的主题名称文本框和主题短名称文本框。

 [ Private Sub DtgvSubject_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DtgvSubject.CellDoubleClick
    Try
        Dim i As Integer
        i = DtgvSubject.CurrentRow.Index
        Me.txtSubjectName.Text = DtgvSubject.Item(1, i).Value
        Me.txtSubSName.Text = DtgvSubject.Item(2, i).Value
    Catch ex As Exception
        MsgBox("No Values in the Cells!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "Alert!!!")
        Button17_Click(sender, e)
    End Try
End Sub]

我的问题是,在获取文本框的值并更新它们、提交更改并刷新我的 datagridview 之后,如何为我的数据库创建一个更新按钮?

【问题讨论】:

  • 请回答我,希望我的帖子可以理解。

标签: vb.net


【解决方案1】:

假设您的输入未绑定到数据集,您只需从数据库中检索现有记录,更新记录并再次保存即可。这样的事情应该可以工作:

public Function updaterecord (field1 as integer, field2 as text) as Object
 Dim db As New EMSDataContext
Dim SaveSubject = From C In db.Subjects
        Where C.SubjectName = txtSubjectName.Text
        Select C
if SaveSubject is not Nothing then
   C.id = field1
   C.name = field2 'etc etc
End if
db.SaveChanges()
return 
From C In db.Subjects
        Where C.SubjectName = txtSubjectName.Text
        Select C
    End Function

【讨论】:

  • 谢谢你,我会试试的
【解决方案2】:

谢谢 Haim 我已经使用 LinqtoSQL 解决了这个问题

 [ Private Sub bnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnUpdate.Click
    If txtClassTeacher.Text = "" Or txtClassName.Text = "" Or txtClassSName.Text = "" Or cboClassSection.Text = "" Then
        MsgBox("Select the data you want Update Please!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "ALERT!!!")
        Exit Sub
    End If
    Dim db As New EMSDataContext
    Dim SaveClasses = From c In db.Classtbls
                      Where c.ID = Val(DtgvClasstbl.CurrentRow.Cells(0).Value)
                      Select c

    For Each c As Classtbl In SaveClasses
        c.Class_Teacher = txtClassTeacher.Text
        c.Class_Name = txtClassName.Text
        c.Short_Name = txtClassSName.Text
        c.Section = cboClassSection.Text
        'Inserting additional changes
    Next
    'Submitting changes to the Database
    Try
        If MsgBox("Are You Sure You Want Update This Record?", MsgBoxStyle.Question + vbYesNo, "Question") = MsgBoxResult.Yes Then
            db.SubmitChanges()
            MsgBox("Data Successfully Updated!", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, "Success!")
            Registration_Load(sender, e)
            txtClassTeacher.Text = ""
            txtClassName.Text = ""
            txtClassSName.Text = ""
            cboClassSection.Text = ""
        Else
            Registration_Load(sender, e)
            txtClassTeacher.Text = ""
            txtClassName.Text = ""
            txtClassSName.Text = ""
            cboClassSection.Text = ""
        End If
    Catch ex As Exception
        MsgBox("Update not Successful!", MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, "ALERT!!!")
        'Making(Adudjstiment)
        db.SubmitChanges()
    End Try
End Sub]

感谢 StackOverflow 和 MSDN 的指导

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-06-11
    相关资源
    最近更新 更多