简体   繁体   English

SQL Server 尝试设置 null 时向值 (char) 添加空格

[英]SQL Server adds empty spaces to value (char) when trying to set null

I am pulling data from SQL Server to my c# project.我正在将数据从 SQL Server 提取到我的 c# 项目。 I have some textboxes that update from page-to-page.我有一些文本框可以逐页更新。 I have one textbox in particular that is set up to only accept two characters, which is setup in the database as char(2) .我有一个特别设置为只接受两个字符的文本框,它在数据库中设置为char(2) If I were to delete those two characters and click my button to update the database and go to the next page, it stores two empty spaces.如果我删除这两个字符并单击我的按钮更新数据库并转到下一页,它会存储两个空格。 I need it to just be empty with no spaces.我需要它是空的,没有空格。 In my other textboxes, this issue does not occur.在我的其他文本框中,不会发生此问题。 The database allows the data to be null.数据库允许数据为空。 I am able to manually enter "null" in the database, but I need it to be done when erasing the two chars and updating it.我可以在数据库中手动输入“null”,但是在擦除两个字符并更新它时需要这样做。

A column declared as CHAR(2) may contain one of the following:声明为CHAR(2)可能包含以下内容之一:

  • 2 characters 2 个字符
  • NULL.空值。

A column declared as CHAR(2) may not contain any of the following:声明为CHAR(2)不得包含以下任何内容:

  • 0 characters 0 个字符
  • 1 character 1 个字符
  • 3 characters 3 个字符
  • etc.等。

When you try to store anything other than either 2 characters or NULL in the column, your database will troll you in the name of some ill-conceived notion of convenience: instead of generating an error, it will store something other than what you gave it to store.当您尝试在列中存储除 2 个字符或 NULL 以外的任何内容时,您的数据库将以某种构思不良的便利概念的名义向您招手:它不会生成错误,而是存储您提供的内容以外的内容储藏。

(Amusingly enough, receiving an error when doing something wrong is, and historically has been, regarded as an inconvenience by a surprisingly large portion of programmers. But that's okay, that's how we get stackoverflow questions to answer.) (有趣的是,在做错事时收到错误消息,而且从历史上看,大多数程序员都认为这是一种不便。但这没关系,这就是我们如何得到 stackoverflow 问题的答案。)

Specifically, your database will pad the value you are storing with spaces, to match the length of the column.具体来说,您的数据库将用空格填充您存储的值,以匹配列的长度。 So, if you try to store just one character, it will add one space.因此,如果您尝试仅存储一个字符,则会添加一个空格。 If you try to store zero characters, it will add two spaces.如果您尝试存储零个字符,则会添加两个空格。

Possible Solutions:可能的解决方案:

  • If you have the freedom to change the type of the column:如果您可以自由更改列的类型:

    Declare it as VARCHAR instead of CHAR(2) , so that it will contain exactly what you store in it.将其声明为VARCHAR而不是CHAR(2) ,以便它将包含您存储在其中的内容。

  • If you do not have the freedom to change the type of the column:如果您不能自由更改列的类型:

    You have to always be manually checking whether you are about to store an empty string into it, and if so, store NULL instead.您必须始终手动检查是否要将空字符串存储到其中,如果是,则存储 NULL。

Note about Oracle甲骨文注意事项

The Oracle RDBMS before version 11g (and perhaps also in more recent versions, I am not sure, if someone knows, please leave a comment) will do that last conversion for you: if you try to store an empty string, it will store NULL instead. 11g 版之前的 Oracle RDBMS(也许还有更新的版本,我不确定,如果有人知道,请发表评论)将为您完成最后一次转换:如果您尝试存储空字符串,它将存储 NULL反而。 This is extremely treacherous due to the following reasons:由于以下原因,这是非常危险的:

  • It is yet one more example of the database system trolling you by storing something very different from what you gave it to store.这是数据库系统通过存储与您提供给它存储的非常不同的东西来控制您的又一个示例。

  • They apply the same rule to all types of character columns, even VARCHAR , which means that you cannot have an empty string even in columns that could accommodate one;他们将相同的规则应用于所有类型的字符列,甚至VARCHAR ,这意味着即使在可以容纳一个的列中也不能有空字符串; you store either NULL or an empty string, you always get NULL back.您存储 NULL 或空字符串,您总是会返回 NULL。

  • This behavior is completely different from the behavior of any other RDBMS.此行为与任何其他 RDBMS 的行为完全不同。

  • The behavior is fixed, there is no way to configure Oracle to quit doing that.该行为已修复,无法将 Oracle 配置为退出该操作。

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

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