我正在尝试根据传递给我的函数的参数从数据库中读取单个条目。我知道连接字符串正在工作,因为我在其他地方使用它,并且我的 SQL 语句工作正常,因为我可以在 SQL Server 中毫无问题地执行它。我遇到的问题是“return”语句。无论我把它放在哪里,我在哪里声明它,或者我把我的 While 循环放在哪里读取数据库表,我都会在“返回 siteID”上得到绿色的波浪线,告诉我“变量 'siteID' 在被赋值之前已被使用”
Public Function GetSiteID(ByVal siteName As String) As String
Dim strConnectionString2 As String = ConfigurationManager.ConnectionStrings("AVDataConnectionString").ConnectionString
Dim strQueryString2 As String = "SELECT SourceSite.SourceSiteID FROM[AVData].[dbo].[SourceSite] JOIN [AVData].[dbo].[SourceSiteMetaTag] ON SourceSite.SourceSiteID=[SourceSiteMetaTag].[SourceSiteID] WHERE SourceSiteMetaTag.TagName='SiteType' AND SourceSiteMetaTag.TagValue='Network'order by [Description] asc AND SiteName = '" & siteName & "'"
Dim connection2 As New SqlConnection(strConnectionString2)
Dim command2 As New SqlCommand(strQueryString2, connection2)
Dim siteID As String
Try
connection2.Open()
Dim myReader As SqlDataReader = command2.ExecuteReader()
While myReader.Read()
siteID = myReader("SourceSiteID").ToString()
End While
Catch ex As Exception
Throw ex
Finally
connection2.Close()
End Try
Return siteID
End Function