SqlCommand 无法识别所选表中的列名

发布于 2024-11-26 07:50:59 字数 495 浏览 0 评论 0原文

我刚刚发布了有关更新连接字符串的上一个问题。它工作正常,但它使我的选择命令失败。 SqlException 被处理为无效的列名 'Username', 'Password', 'UserType' 它们已经在我的数据库中的 User 表中。

下面是我的选择命令:

myConnection = New SqlConnection("Server=RAVY-PC\RAVY;Database=CIEDC;Trusted_Connection=True")
myConnection.Open()

myCommand = New SqlCommand("SELECT UserType FROM User WHERE UserName = @User AND Password = @Pass", myConnection)

txtUserType.Text = myCommand.ExecuteScalar()

由于它捕获了上述错误,我不知道问题是什么。请帮忙。

I just posted a previous question about updating the connection string. It works fine, but it makes my select command fail. The SqlException was handled that Invalid column name 'Username', 'Password', 'UserType' which they are already in my User table from my database.

Below is my select command:

myConnection = New SqlConnection("Server=RAVY-PC\RAVY;Database=CIEDC;Trusted_Connection=True")
myConnection.Open()

myCommand = New SqlCommand("SELECT UserType FROM User WHERE UserName = @User AND Password = @Pass", myConnection)

txtUserType.Text = myCommand.ExecuteScalar()

Since it catched the above error, I've no idea what was the problem. Please help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲世九天 2024-12-03 07:50:59

尝试在 User 周围放置方括号来引用表名(User 是关键字):

myCommand = New SqlCommand("SELECT UserType FROM [User] WHERE UserName = @User AND Password = @Pass", myConnection)

Try placing square backets around User to quote the tablename (User is a keyword):

myCommand = New SqlCommand("SELECT UserType FROM [User] WHERE UserName = @User AND Password = @Pass", myConnection)
℉服软 2024-12-03 07:50:59

您似乎没有设置参数:

        myCommand.Parameters.Add("@User","MyUsername")
    myCommand.Parameters.Add("@Pass","MyPassword")

It doesn't look like you are setting your parameters:

        myCommand.Parameters.Add("@User","MyUsername")
    myCommand.Parameters.Add("@Pass","MyPassword")
调妓 2024-12-03 07:50:59

使用这个:

myCommand = New SqlCommand("SELECT * FROM [User] WHERE UserName = @User AND Password = @Pass", myConnection)

然后添加参数。

Use this:

myCommand = New SqlCommand("SELECT * FROM [User] WHERE UserName = @User AND Password = @Pass", myConnection)

Then add parameters.

深爱成瘾 2024-12-03 07:50:59

在列名称周围使用方括号。这将允许关键字用作列名称。例如尝试以下操作。

select [UserType] from tblStaff

Use square brackets around your column names. This will allow keywords to be used as column names. Try the following for example.

select [UserType] from tblStaff
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文