vb.net - 对象引用未设置到对象实例
当我尝试在 sql 语句中使用参数时出现此错误,但在不使用它时它工作正常。我的代码如下:
Dim i As String
Dim sql as String
sql = "SELECT * FROM tblStaff WHERE Username = @User AND Password = @Pass"
myCommand = New SqlCommand(sql, myConnection)
myCommand.Parameters.AddWithValue("@User", txtUser.Text)
myCommand.Parameters.AddWithValue("@Pass", txtPassword.Text)
i = myCommand.ExecuteScalar
txtUserType.Text = i.ToString
当我评论 txtUserType.Text = i.ToString 时,它工作正常。有什么想法吗?
I got this error when trying to use parameters to my sql statement, but it works fine when not use it. My codes are below:
Dim i As String
Dim sql as String
sql = "SELECT * FROM tblStaff WHERE Username = @User AND Password = @Pass"
myCommand = New SqlCommand(sql, myConnection)
myCommand.Parameters.AddWithValue("@User", txtUser.Text)
myCommand.Parameters.AddWithValue("@Pass", txtPassword.Text)
i = myCommand.ExecuteScalar
txtUserType.Text = i.ToString
And when I comment on txtUserType.Text = i.ToString
, it works fine. Any idea?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
ExecuteScalar 应该只返回一个值,例如整数。因此,如果您在 SQL 语句中仅指定一列,例如(Select usertype from tblStaff...),executescalar 应返回一个整数(如果该列是数字)。
那么它应该可以工作。
顺便说一句..您不必在字符串变量上使用 ToString 。只需使用变量名称
txtUserType.Text = 我
ExecuteScalar should only give you one value back, like an integer. So if you specify only one column in your SQL statement for example (Select usertype from tblStaff...) the executescalar should return an integer (if that column is a number).
Then it should work.
and by the way.. you don't have to use ToString on a variable that is a string. Just use the variables name
txtUserType.Text = i