【问题标题】:It says I am inserting too many values in my columns when I am not? [closed]它说我不是在我的列中插入了太多值? [关闭]
【发布时间】:2021-12-10 23:23:09
【问题描述】:

我正在创建一个登录菜单并输入详细信息:

Username:test2
Email:Test2
Password: testPassword
Confirm Password: testPassword

确认密码必须与进入数据库的密码相同,所以它是一个值

然后我得到错误:

System.Data.SqlClient.SqlException:'无效的列名'test2test2testPassword'。 INSERT 语句中的列多于 VALUES 子句中指定的值。 VALUES 子句中的值数必须与 INSERT 语句中指定的列数匹配。'

有人可以帮忙吗?

【问题讨论】:

  • 查看您生成的 SQL。它可能看起来像... VALUES(usernameuser@domain.compassword)。您忘记输入任何逗号或引用您的值。事实上,无论如何你都不应该构建这样的 SQL 查询——听说过 SQL 注入吗?使用参数化查询。
  • 其次,请将代码放入code sections而不是代码图像。
  • 如果你真的看过你生成的SQL,问题就很明显了。
  • 您还应该使用using 处理您的连接和命令

标签: c# sql database


【解决方案1】:

cmets 很好地涵盖了它,但是您在查询中缺少值之间的分隔 最重要的是,您不应信任用户输入的文本并将其直接用于查询

你应该使用像这样的参数化命令

var checkCount =
    new SqlCommand("insert into [TableName] (id, othercolumn) VALUES(@id, @otherColumn)",
                    conn);
checkCount.Parameters.AddWithValue("@id", id);
checkCount.Parameters.AddWithValue("@otherColumn", otherColumn);

【讨论】:

    【解决方案2】:

    您的代码中存在多个问题:

    1. 您应该在查询中使用参数:https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.parameters?view=dotnet-plat-ext-5.0
    2. 值应以逗号分隔:
    insert into RegisterTable(RegUsername, RegEMail, RegPassword)
                       Values('test2','test2','testPassword')
    
    1. 永远不要在您的数据库中存储未散列的密码。见Best way to store password in database

    【讨论】:

    • 而且,存储密码而不首先对其进行哈希处理会邀请网络黑客入侵您的用户。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-21
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 2021-08-23
    • 1970-01-01
    相关资源
    最近更新 更多