关于主题,我已经研究过,找不到与我的问题相同的解决方案。请参阅下面我的代码,其中的错误应该来自:
Public Class Athletedb
Shared athlete As New Athlete
Public Shared Function GetAthletes(Dir As String) As List(Of Athlete)
Dim athleteList As New List(Of Athlete)
Dim connection As OleDbConnection = AthleteManDB.GetConnection()
Dim selectStatement As String =
"SELECT Name, Surname, BirthDate, RegDate, Gender, Amount, MemberNumber" &
"FROM Athletes" &
"ORDER BY Surname"
Dim selectCommand As OleDbCommand(selectStatement, connection)
Try
connection.Open()
Dim textIn As OleDbDataReader = OleDbCommand.ExecuteReader()
Dim athlete As Athlete
Do While textIn.Read
athlete = New Athlete
athlete.Name = textIn("Name").ToString
athlete.Surname = textIn("Surname").ToString
athlete.Birthdate = textIn("Birthdate").ToString
athlete.RegDate = textIn("RegDate").ToString
athlete.Gender = textIn("Gender").ToString
athlete.Amount = textIn("Amount").ToString
athlete.MemberNumber = textIn("MemberNumber").ToString
athleteList.Add(athlete)
Loop
textIn.Close()
Catch ex As OleDbException
Throw ex
Finally
connection.Close()
End Try
Return athleteList
End Function
Public Shared Function SaveAthlete(Dir As String, athletes As List(Of Athlete))
Dim connection As OleDbConnection = AthleteManDB.GetConnection
Dim insertStatement As String =
"INSERT Athletes" &
"(Name, Surname, Birthdate, RegDate, Gender, Amount, MemberNumber)" &
"VALUES (@Name, @Surname, @Birthdate, @RegDate, @Gender, @Amount, @MemberNumber)"
Dim insertCommand As New OleDbCommand(insertStatement, connection)
insertCommand.Parameters.AddWithValue("@Name", athlete.Name)
insertCommand.Parameters.AddWithValue("@Surname", athlete.Name)
insertCommand.Parameters.AddWithValue("@Birthdate", athlete.Name)
insertCommand.Parameters.AddWithValue("@RegDate", athlete.Name)
insertCommand.Parameters.AddWithValue("@Gender", athlete.Name)
insertCommand.Parameters.AddWithValue("@Amount", athlete.Name)
insertCommand.Parameters.AddWithValue("@MemberNumber", athlete.Name)
Try
connection.Open()
insertCommand.ExecuteNonQuery()
Dim selectStatement As String =
"SELECT INDENT_CURRENT('Athletes') FROM Athletes"
Dim selectCommand As New OleDbCommand(selectStatement, connection)
Catch ex As OleDbException
Throw ex
Finally
connection.Close()
End Try
Return athletes
End Function
Public Shared Function DeleteAthlete(Dir As String, athletes As List(Of Athlete)) As Boolean
Dim connection As OleDbConnection = AthleteManDB.GetConnection
Dim deleteStatement As String =
"DELETE FROM Athletes " &
"WHERE Name = @Name " &
"WHERE Surname = @Surname " &
"WHERE Birthdate = @Birthdate " &
"WHERE RegDate = @RegDate " &
"WHERE Gender = @Gender " &
"WHERE Amount = @Amount " &
"WHERE MemberNumber = @MemberNumber "
Dim deleteCommand As New OleDbCommand(deleteStatement, connection)
deleteCommand.Parameters.AddWithValue("@Name", athlete.Name)
deleteCommand.Parameters.AddWithValue("@Surname", athlete.Name)
deleteCommand.Parameters.AddWithValue("@Birthdate", athlete.Name)
deleteCommand.Parameters.AddWithValue("@RegDate", athlete.Name)
deleteCommand.Parameters.AddWithValue("@Gender", athlete.Name)
deleteCommand.Parameters.AddWithValue("@Amount", athlete.Name)
deleteCommand.Parameters.AddWithValue("@MemberNumber", athlete.Name)
Try
connection.Open()
Dim count As Integer = deleteCommand.ExecuteNonQuery
If count > 0 Then
Return True
Else
Return False
End If
Catch ex As OleDbException
Throw ex
Finally
connection.Close()
End Try
End Function
结束班
我也得到一个非共享成员的引用,需要在下面的行上引用对象并且不确定那是什么
Dim textIn As OleDbDataReader = OleDbCommand.ExecuteReader()
答案 0 :(得分:0)
由于OleDBCommand类是静态的,因此不允许在不先创建实例的情况下使用它。由于您已经创建了它的一个实例(selectCommand),您应该使用它:
textIn = selectCommand.ExecuteReader()