Member 12736445 Ответов: 2

Свойство connectionString не инициализировано. В чем может быть проблема


Imports System.Data.OleDb

Public Class MyAthleteEvents

    Public Shared Function GetConnection() As OleDbConnection
       
        Dim connectionString As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\lebom\Desktop\ICT3611Assignment\AthletesEvents.accdb")

        Return New OleDbConnection()
    End Function

End Class


Imports System.Data.OleDb

Public Class MyeventDB

    Public Shared Function GetEvent(eventID As Integer) As Myevent
        Dim myevent As New Myevent
        Dim connection As OleDbConnection = MyAthleteEvents.GetConnection
        Dim selectStatement As String =
            "SELECT EventID, Title, Date, Fee, Location, Distance " &
            "FROM Events " &
            "WHERE EventID = @EventID"
        Dim selectCommand As New OleDbCommand(selectStatement, connection)
        selectCommand.Parameters.AddWithValue("@EventID", eventID)
        Try
            connection.Open()
            Dim reader As OleDbDataReader _
                = selectCommand.ExecuteReader(CommandBehavior.SingleRow)
            If reader.Read Then
                myevent.EventID = CInt(reader("EventID"))
                myevent.Title = reader("Title").ToString
                myevent.DDate = reader("Date").ToString
                myevent.Fee = reader("Fee").ToString
                myevent.Location = reader("Location").ToString
                myevent.Distance = reader("Distance").ToString
            Else
                myevent = Nothing
            End If
            reader.Close()
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
        Return myevent
    End Function

    Public Shared Function AddEvent(myevent As Myevent) As Integer
        Dim connection As OleDbConnection = MyAthleteEvents.GetConnection
        Dim insertStatement As String =
            "INSERT Events " &
            "(Title, Date, Fee, Location, Distance) " &
            "VALUES (@Title, @Date, @Fee, @Location, @Distance)"
        Dim insertCommand As New OleDbCommand(insertStatement, connection)
        insertCommand.Parameters.AddWithValue("@Title", myevent.Title)
        insertCommand.Parameters.AddWithValue("@Date", myevent.DDate)
        insertCommand.Parameters.AddWithValue("@Fee", myevent.Fee)
        insertCommand.Parameters.AddWithValue("@Location", myevent.Location)
        insertCommand.Parameters.AddWithValue("@Distance", myevent.Distance)
        Try
            connection.Open()
            insertCommand.ExecuteNonQuery()
            Dim selectStatement As String _
                = "SELECT IDENT_CURRENT('Events') FROM Events"
            Dim selectCommand As New OleDbCommand(selectStatement, connection)
            Dim eventID As Integer = CInt(selectCommand.ExecuteScalar)
            Return eventID
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
    End Function

    Public Shared Function UpdateEvent(oldEvent As Myevent,
            newEvent As Myevent) As Boolean
        Dim connection As OleDbConnection = MyAthleteEvents.GetConnection
        Dim updateStatement As String =
            "UPDATE Events SET " &
            "Title = @NewTitle, " &
            "Date = @NewDate, " &
            "Fee = @NewFee, " &
            "Location = @NewLocation, " &
            "Distance = @NewDistance " &
            "WHERE EventID = @OldEventID " &
            "AND Title = @OldTitle " &
            "AND Date = @OldDate " &
            "AND Fee = @OldFee " & "AND Location = @OldLocation " &
            "AND Distance = @OldDistance"
        Dim updateCommand As New OleDbCommand(updateStatement, connection)
        updateCommand.Parameters.AddWithValue("@NewTitle", newEvent.Title)
        updateCommand.Parameters.AddWithValue("@NewDate", newEvent.DDate)
        updateCommand.Parameters.AddWithValue("@NewFee", newEvent.Fee)
        updateCommand.Parameters.AddWithValue("@NewLocation", newEvent.Location)
        updateCommand.Parameters.AddWithValue("@NewDistance", newEvent.Distance)
        updateCommand.Parameters.AddWithValue("@OldEventID", oldEvent.EventID)
        updateCommand.Parameters.AddWithValue("@OldTitle", oldEvent.Title)
        updateCommand.Parameters.AddWithValue("@OldDate", oldEvent.DDate)
        updateCommand.Parameters.AddWithValue("@OldFee", oldEvent.Fee)
        updateCommand.Parameters.AddWithValue("@OldLocation", oldEvent.Location)
        updateCommand.Parameters.AddWithValue("@OldDistance", oldEvent.Distance)

        Try
            connection.Open()
            Dim count As Integer = updateCommand.ExecuteNonQuery
            If count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
    End Function

    Public Shared Function DeleteEvent(myevent As Myevent) As Boolean
        Dim connection As OleDbConnection = MyAthleteEvents.GetConnection
        Dim deleteStatement As String =
            "DELETE FROM Events " &
            "WHERE EventID = @EventID " &
            "AND Title = @Title " &
            "AND Date = @Date " &
            "AND Fee = @Fee " &
            "AND Location = @Location " &
            "AND Distance = @Distance"
        Dim deleteCommand As New OleDbCommand(deleteStatement, connection)
        deleteCommand.Parameters.AddWithValue("@EventID", myevent.EventID)
        deleteCommand.Parameters.AddWithValue("@Title", myevent.Title)
        deleteCommand.Parameters.AddWithValue("@Date", myevent.DDate)
        deleteCommand.Parameters.AddWithValue("@Fee", myevent.Fee)
        deleteCommand.Parameters.AddWithValue("@Location", myevent.Location)
        deleteCommand.Parameters.AddWithValue("@Distance", myevent.Distance)

        Try
            connection.Open()
            Dim count As Integer = deleteCommand.ExecuteNonQuery
            If count > 0 Then
                Return True
            Else
                Return False
            End If
        Catch ex As Exception
            Throw ex
        Finally
            connection.Close()
        End Try
    End Function

End Class


Что я уже пробовал:

Я пытался отлаживать и изменять код, но все еще не могу справиться

2 Ответов

Рейтинг:
2

Estys

В вашем

GetConnection
функция говорит он:

Return New OleDbConnection()


Это возвращает неинициализированное соединение.

Он должен читать :

Return connectionString


Рейтинг:
0

Maciej Los

Причина этого "The connectionstring property has not been initialized"здесь кроется ошибка:

Dim connection As OleDbConnection = MyAthleteEvents.GetConnection


MyAthleteEvents это класс, который должен быть инициализирован с помощью New ключевое слово.

Это должно решить вашу проблему:
Dim mae As MyAthleteEvents = new MyAthleteEvents()
Dim connection As OleDbConnection = mae.GetConnection


Но я бы избегал такой практики программирования. Вместо этого, пожалуйста, прочтите о ДАЛМАТИНЕЦ[^].