我在网上找到了代码,这对我正在处理的项目有很大帮助,如果有人可以推荐修复它真的会受到赞赏:
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim cmd As OleDb.OleDbCommand
If Not cnn.State = ConnectionState.Open Then
'open connection if it is not yet open
cnn.Open()
End If
以下行是有错误的行,变量'cmd'在分配值之前使用:
cmd.Connection = cnn
'check whether add new or update
If Me.tbMemNo.Tag & "" = "" Then
'add new
'add data to table
cmd.CommandText = "INSERT INTO athlete(memno, firstname, lastname, dob, gender, joindate, memfeeout) " &
" VALUES(" & tbMemNo.Text & ",'" & tbFirstName.Text & "','" & tbLastName.Text & "','
" & dpDOB.Text & "','" & cboGender.Text & "','" & dpClubJoinDate.Text & "','
" & tbMemFeeOut.Text & "')"
cmd.ExecuteNonQuery()
Else
'update data in table
cmd.CommandText = "Update athlete " &
" SET memno = " & tbMemNo.Text &
", firstname = '" & tbFirstName.Text & "'" &
", lastname = '" & tbLastName.Text & "'" &
", dob = '" & dpDOB.Text & "'" &
", gender = '" & cboGender.Text & "'" &
", joindate = '" & dpClubJoinDate.Text & "'" &
", memfeeout = '" & tbMemFeeOut.Text & "'" &
" WHERE memno = " & tbMemNo.Tag
cmd.ExecuteNonQuery()
End If
'Refresh data in list
RefreshData()
'clear form
btnClear.PerformClick()
'close connection
cnn.Close()
End Sub
由于