Vb.net 为什么我会得到一个“a”;“没有数据存在”;传递“0”后出错;“哈斯罗”;测试?
我有这个密码:Vb.net 为什么我会得到一个“a”;“没有数据存在”;传递“0”后出错;“哈斯罗”;测试?,vb.net,invalidoperationexception,no-data,Vb.net,Invalidoperationexception,No Data,我有这个密码: Protected Function GetArgValsForCompanyName(coName As String) As String() Dim args(2) As String Dim sqlConnection1 As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum") Dim cmd As New SqlCommand
Protected Function GetArgValsForCompanyName(coName As String) As String()
Dim args(2) As String
Dim sqlConnection1 As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum")
Dim cmd As New SqlCommand
Dim reader As SqlDataReader
cmd.CommandText = "select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName"
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName
cmd.Connection = sqlConnection1
sqlConnection1.Open()
reader = cmd.ExecuteReader()
If reader.HasRows Then
args(0) = reader.Item(0).ToString()
args(1) = reader.Item(1).ToString()
args(2) = reader.Item(2).ToString()
End If
reader.Close()
sqlConnection1.Close()
Return args
End Function
…在这一行失败:
args(0) = reader.Item(0).ToString()
…与:
*System.InvalidOperationException was unhandled
HResult=-2146233079
Message=Invalid attempt to read when no data is present.*
怎么可能它“有行”却没有数据
注意:当我尝试此操作(而不是使用“0”索引)时,它也会失败,并出现相同的错误:
更新
被接受的答案在现代应用程序(例如,我的“沙盒”Windows窗体应用程序)中运行良好,但在旧的应用程序中,如使用.NET史前版本的破旧网站,则不起作用-显然无法识别“使用”;我得到:
Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request.
Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30203: Identifier expected.
Source Error:
Line 90: Dim args(2) As String
Line 91:
Line 92: Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"),
Line 93: cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE
CompanyName = @CoName", con)
Line 94:
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491
还需要调用“read”;这起作用(可以使用返回字段的名称或其索引):
正如您在中所说,需要调用.Read
但是,If reader.HasRows
不是必需的。Do While reader.Read将处理此问题。如果有行,则它将进入循环,否则它将绕过
作为补充说明,我认为实施以下措施将是有益的:
使用时,您不必担心调用.Close
或处理对象。我也觉得它读起来更好。要解决这个问题,使用一个不能用
处理的古老编译器,你可以用试试
-最后
手工实现它。
Server Error in '/EMS/customerreportingnet' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request.
Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30203: Identifier expected.
Source Error:
Line 90: Dim args(2) As String
Line 91:
Line 92: Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"),
Line 93: cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE
CompanyName = @CoName", con)
Line 94:
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.5485; ASP.NET Version:2.0.50727.5491
If reader.HasRows Then
Do While reader.Read
'args(0) = reader.Item(0).ToString()
args(0) = reader.Item("Unit").ToString()
args(1) = reader.Item(1).ToString()
args(2) = reader.Item(2).ToString()
Loop
End If
Protected Function GetArgValsForCompanyName(coName As String) As String()
Dim args(2) As String
Using con As New SqlConnection("SERVER=PLATYPUS42;DATABASE=duckbilldata;UID=durante;PWD=pondscum"),
cmd As New SqlCommand("select Unit, MemberNo, CustNo from Customers WHERE CompanyName = @CoName", con)
con.Open()
cmd.CommandType = CommandType.Text
cmd.Parameters.Add("@CoName", SqlDbType.VarChar, 50).Value = coName
Using reader As SqlDataReader = cmd.ExecuteReader
While reader.Read
args(0) = reader.Item(0).ToString()
args(1) = reader.Item(1).ToString()
args(2) = reader.Item(2).ToString()
End While
End Using
End Using
Return args
End Function