我有一个函数来获取有关SQL Server表的信息。我知道函数是正确的并且返回所有内容(我使用msgBox来检查它)。问题是,当我尝试使用该功能时,它会显示错误:
我的WCF功能:
Public Function GetCLInfo(ByVal cl_ID As Integer) Implements SQLOperator.GetCLInfo
Dim CLInfo(7) As String
Try
SQLCon.ConnectionString = SQLConString
SQLCon.Open()
SQLCMD = New SqlCommand("Select * from dbo.cl Where ID=" & cl_ID & ";", SQLCon)
SQLDataReader = SQLCMD.ExecuteReader()
While SQLDataReader.Read
CLInfo(0) = SQLDataReader("ID")
CLInfo(1) = SQLDataReader("Name")
CLInfo(2) = SQLDataReader("Address")
CLInfo(3) = SQLDataReader("Contact")
End While
Return CLInfo
Catch ex As Exception
MsgBox("Error: " & Environment.NewLine & ex.ToString)
CLInfo(0) = "False"
Return CLInfo
Finally
SQLCon.Close()
SQLCon.Dispose()
End Try
Return CLInfo
End Function
在图片中,您可以看到我是如何尝试使用该功能的。
有人可以告诉我我做错了什么吗?
答案 0 :(得分:0)
你的问题是你正在调用一个函数来返回字符串数组而程序本身并没有捕获。正如@Matt Wilko指出的那样使用Option Strict On
。
其次,如果您有多个客户端,那么您的逻辑就会失败。
For I as integer = 1 to AllCl
Dim cl(7) as String
cl = Service.GetClInfo(I)
next
上面的代码每次都会重置,加上当你离开循环时,你将无法访问cl。
丑陋的修复可能是multidimensional array
。
现在您应该指定返回类型
Public Function GetCLInfo(ByVal cl_ID As Integer) As String() Implements SQLOperator.GetCLInfo