简体   繁体   English

列名无效 C# SQL 语句

[英]Invalid column name C# SQL statement

At first, I got error that USER was incorrect syntax and it should be "[USER]" instead.起初,我收到错误,说 USER 的语法不正确,应该是“[USER]”。 Now I get that the ex: {"Invalid column name 'USER'."} .现在我知道ex: {"Invalid column name 'USER'."} I don't know what to do, it seems that I either get one error or the other.我不知道该怎么办,似乎我要么得到一个错误,要么得到另一个错误。

private void btnEDIT_Click(object sender, EventArgs e)
{
    string _ID = lblUserID.Text; // THESE ARE TO COLLECT THE INFORMATION FROM THE TEXTBOXES IN WHICH YOU EDIT THE "USERS"
    string _user = tbUser.Text.Trim();
    string _password = tbPassword.Text.Trim().ToLower();
    string _role = tbRole.Text.Trim();

    editDataGrid(_user, _password, _role); // THIS IS FOR UPDATING MY GRID AT THE SAME TIME

    string str = "Update UserValidation SET [USER] = '" + _user + "', PASSWORD = '" + _password + "', ROLE = '" + _role + "' WHERE userID ='" + _ID + "'";
    clsDB.InsUpDel(str); // MY METHOD FOR EXECUTING SQL STATEMENTS
}

You could check the scope of your query.您可以检查查询的范围。 As in, for sure your UserValidation is in the selected database instance you are using on your connection string.如,确保您的 UserValidation 位于您在连接字符串上使用的选定数据库实例中。 Also make sure the that column name is USER and not User or some variation.还要确保该列名称是 USER 而不是 User 或某些变体。

Try to run normal query to be sure all params and columns are correct, then replace param with your field, 3rd are you sure userId is string as you write?尝试运行普通查询以确保所有参数和列都正确,然后将参数替换为您的字段,第三您确定 userId 在您编写时是字符串吗?

UPDATE UserValidation 
SET USER = 'xxxx', PASSWORD = 'xxxx', ROLE = 'xxxx' 
WHERE userID ='xxxx';

I recommend to use parameters instead of concatenating together your query ... + 'pram1' + ... + 'paramN'..我建议使用参数而不是将您的查询连接在一起... + 'pram1' + ... + 'paramN'..

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM