Я не знаю, откуда у вас шаблон для этой функции, но он очень запутан.Очевидно, вы пытаетесь подключиться к базе данных Sql Server, но я не вижу никакого соединения в вашем коде.
Сначала давайте рассмотрим ваш код.
'Good name for your function
Public Function getSectionLength(sectionUID As Integer) As Integer
Dim sectionLength As Integer = 0
'The DataReader constructor does take any arguments.
'You should be using an SqlDataReader
'Normally you do not need a New DataReader because .ExecuteReader returns a DataReader
'Good use of Using
Using dr As New DataReader(Globals.dif)
Dim SQL As String = "SELECT dbo.SECTION_ATTRIBUTES.SECTION_LENGTH
FROM dbo.SECTION_ATTRIBUTES WHERE dbo.SECTION_ATTRIBUTES.SECTION_UID =
@sectionUid"
'Commands provides its own collection called Parameters
Dim paramList As New List(Of SqlClient.SqlParameter)
paramList.Add(New SqlClient.SqlParameter("@sectionUid",sectionUID))
'The only argument that .ExecuteReader takes is a CommandBehavior enumeration
'.ExecutleReader won't do anything
dr.Execut1eReader(SQL, paramList)
If dr.Read Then
sectionLength = dr("SECTION_LENGTH")
End If
End Using
Return sectionLength
End Function
Это возможная заменадля вашего кода.Вам нужно добавить Imports System.Data.SqlClient
в начало вашего файла.
Private Function GetSectionLength(SectionUID As Integer) As Integer
Dim sectionLength As Integer = 0
'Pass your connection string to the constructor of the connection
Using cn As New SqlConnection("Your connecion string")
'pass your sql statement and the connection directly to the constructor of the command
Using cmd As New SqlCommand("SELECT dbo.SECTION_ATTRIBUTES.SECTION_LENGTH
FROM dbo.SECTION_ATTRIBUTES
WHERE dbo.SECTION_ATTRIBUTES.SECTION_UID = @sectionUid", cn)
'Use the .Add method of the commands Parameters collection
cmd.Parameters.Add("@sectionUid", SqlDbType.Int).Value = SectionUID
'Open the connection at the last possible moment
cn.Open()
'.ExecuteScalar returns a single value, the first column of the first row of your query result
sectionLength = CInt(cmd.ExecuteScalar)
End Using 'Closes and disposes the command
End Using 'closes and disposes the connection
Return sectionLength
End Function