本文介绍了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
我尝试了什么:
我试图调试和更改代码,但仍然无法通过
What I have tried:
I tried to debug and change code but still cant pull through
推荐答案
GetConnection
它说的功能:
function it says:
Return New OleDbConnection()
返回未初始化的连接。
应该是:
That returns an uninitialized connection.
It should read :
Return connectionString
Dim connection As OleDbConnection = MyAthleteEvents.GetConnection
MyAthleteEvents
是一个必须用 New
关键字初始化的类。 />
这应解决您的问题:
MyAthleteEvents
is a class which must be initialized with New
keyword.
This should resolve your issue:
Dim mae As MyAthleteEvents = new MyAthleteEvents()
Dim connection As OleDbConnection = mae.GetConnection
但是我会避免这种编程习惯。而不是它,请阅读 []。
这篇关于connectionstring属性尚未初始化。可能是什么问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!