有时候会出现报错「'項目名’はテーブルに属していません」
排查原因:
①项目名写错>>>多用复制粘贴。手打易错。
②忘记设置DT>>>不写在Form_load处,易忘。写在DataRow赋值前,肯定不会忘。
例:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim tt As String
Dim tt2 As Date
Dim ii As Integer
If MsgBox("予約しますか", vbYesNo, "") = vbYes Then
Try
CM.CommandText = "Select * from dbo.reserves"
DA.SelectCommand = CM
DA.Fill(DS, "reserves")
DT = DS.Tables("reserves")
DR = DT.NewRow()
tt = Format(Now(), "yyMMddHHmmss")
ii = DataGridView1.CurrentRow.Index
tt2 = DataGridView1.Rows(ii).Cells(1).Value
DR("ReserveID") = tt + dd
DR("EventDate") = tt2
DR("EventTime") = DataGridView1.Rows(ii).Cells(2).Value.ToString()
DR("EventRoom") = DataGridView1.Rows(ii).Cells(3).Value
DR("EventName") = DataGridView1.Rows(ii).Cells(4).Value.ToString()
DR("Applicant") = L + " " + F
DT.Rows.Add(DR)
CM.CommandText = "insert into dbo.reserves values (" &
"'" & DR("ReserveID") &
"',CONVERT(DATETIME, '" & DR("EventDate").ToString & "',111" &
"),'" + DR("EventTime") &
"','" & DR("EventRoom") &
"','" & DR("EventName") &
"','" & DR("Applicant") &
"')"
CM.ExecuteNonQuery()
MsgBox("予約できました")
reserve_list.Show()
Catch ex As Exception
MsgBox(ex.Message)
Finally
If DRR.IsClosed = False Then
DRR.Close()
End If
End Try
CN.Close()
Else
MsgBox("予約できませんでした")
End If
End Sub