1

我正在编写一个带有 ASP.Net 前端、SQL 服务器后端的 C#、.Net3.5、3 层 Web 应用程序,并通过 TableAdapters 和存储过程与它们交互。我偶然发现了一些奇怪的功能。我有一个包含列的 AdminUsers 表

UserID int NOT NULL
Username varchar(20) NOT NULL
Hash varchar(50) NOT NULL
Salt varchar(50) NOT NULL
Email  varchar(50) NOT NULL

作为登录身份验证过程的一部分,在比较密码之前,我会在表格中搜索提供的用户名。这失败了,因为表中每个 varchar 列的每个条目,当通过 TableAdapter GetData 语句返回时,都用空格填充到 15 个字符,即。

登录用户名 = “乔”

存储用户名 = “乔

有趣的是,存储在数据库中的值是正确的,我跟踪了一行的插入以确保这一点。显示此表的 Datagrid 也可以正确显示。该问题仅在我调用以下方法时出现,

public static bool CheckIfUsernameAvailable(string username) {
        AdminUsersDataTable data = DataCalls.GetAdministers();
        foreach (Data.MissionEducate.AdminUsersRow row in data) {
            if (row.Username == username) {
                return false;
            }
        }
        return true;
    }

public static Data.MissionEducate.AdminUsersDataTable GetAdministers() {
        AdminUsersTableAdapter tblApt = new AdminUsersTableAdapter();
        return tblApt.GetData();
    }

GetData() 基本上调用 SELECT * FROM AdminUsers

有什么建议为什么要在 tableAdapters 选择命令结果的末尾附加空格?数据似乎不是这样存储的。表适配器是否经常显示此行为?我还可以提供哪些信息?

非常感谢。

4

1 回答 1

1

It appears the history of the table would provide the information needed to solve this issue. When original creating the DB Table I used the nchar type instead of varchar. Nchar obviously being a fixed length, would pad the values stored within it with enough spaces to be the specified length (15). Funnily enough, I had figured this, and altered the table, deleted and reinserted the values, deleted and recreated the table adapter, but the problem persisted. I solved the issue, however, by dropping the table, deleting the table adapter, deleting associated stored procs, creating a new table with a different name, creating new table adapter with new name, created new stored procs with new name, and solution solved. Fortunately the app is still only in prod, so not too much time lost, but I obviously couldn't have done this with a live app. I don't know what I was missing to allow the nchar's length restriction to persist throughout all my changes.

于 2011-02-25T00:57:16.533 回答