我的“更新”按钮有问题,我不知道从哪里开始更新我的SQL数据库,我使用SSMS Microsoft创建我的表主题,我还将它与我的VS 2010 ULTIMATE连接在一起我将我的表拖到LINQtoSQL和DATASET,我创建了一个Save按钮,它成功保存到我的数据库并刷新我的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后,如何为数据库创建更新按钮?
答案 0 :(得分:0)
假设您的输入未绑定到数据集,您只需要从数据库中检索现有记录,更新记录并再次保存。这样的事情应该有效:
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
答案 1 :(得分:0)
谢谢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提供指导