[英]Overload resolution failed because no accessible 'ExecuteScalar' accepts this number of arguments
Im pretty new to VB.NET and was wondering how I'd fix this error im getting. 我是VB.NET的新手,我想知道如何解决此错误。
CODE 码
Dim sql As String = "SELECT (USER) FROM USER_CONSTANT"
Dim conn As New SqlConnection(strConn)
Dim objDR As SqlDataReader
Dim Cmd As New SqlCommand(sql, conn)
conn.Open()
objDR = Cmd.ExecuteScalar(CommandBehavior.CloseConnection)
While objDR.Read()
Label7.Text = objDR("USER")
The ExecuteScalar method doesn't expect any parameter. ExecuteScalar方法不需要任何参数。 The CommandBehavior enum could be passed to the ExecuteReader method, so I suppose that your code should be written as
可以将CommandBehavior枚举传递给ExecuteReader方法,因此我想您的代码应编写为
Dim sql As String = "SELECT (USER) FROM USER_CONSTANT"
Using conn = New SqlConnection(strConn)
Using Cmd = New SqlCommand(sql, conn)
conn.Open()
Using objDR = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
While objDR.Read()
Label7.Text = objDR("USER")
End While
End Using
End Using
End Using
It is not clear if you expect to receive more than one record from this code. 尚不清楚您是否希望从此代码中收到多个记录。 In case of multiple records returned then note that at every loop you change the content of the Label7.Text with the current USER and your loop ends with the last name displayed
如果返回多个记录,则请注意,在每个循环中,您都使用当前的USER更改Label7.Text的内容,并且循环以显示的姓氏结束
ExecuteScalar
does not accept any variables (see documentation ). ExecuteScalar
不接受任何变量(请参阅文档 )。 just change the line to objDR = Cmd.ExecuteScalar
. 只需将行更改为
objDR = Cmd.ExecuteScalar
。
Just as the error says, there's no overload of ExecuteScalar
which accepts that number of arguments. 正如错误所言,
ExecuteScalar
不会接受任何数量的参数重载。 Or any arguments . 或任何论点 。 Instead:
代替:
objDR = Cmd.ExecuteScalar()
Or did you mean to use ExecuteReader
, since you're intending to loop over the results and a scalar isn't a result set? 还是您打算使用
ExecuteReader
,因为您打算遍历结果并且标量不是结果集?
objDR = Cmd.ExecuteReader(CommandBehavior.CloseConnection)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.