boolean.ToString() 在 .NET 2.0 和 .NET 2.0 之间的行为是否不同? .NET 3.5

发布于 2024-09-25 02:03:36 字数 1024 浏览 4 评论 0原文

我在工作中继承了一段代码,我刚刚注意到它有一个相当微不足道但奇怪的小怪癖。所以也许有人可以帮我设置干净的 .NET 2.0 环境来测试它。

有一个 SQL2K5 表,其中包含名为 IsEnabled BIT NOT NULL 的列

有一个从该表中选择的存储过程

有一段 C# 代码,使用 SQL Reader 调用该存储过程,循环遍历结果行,并将其中一些信息写入日志文件以及执行其他操作。

using (SqlDataReader reader = DAL.SqlHelper.ExecuteReader(connStr, "usp_MySproc"))
{
    while (reader.Read())
    {
         //code to do stuff...

         string s = reader["IsEnabled "].ToString(); //exactly like this...

         //code to concatenate `s` with other values
         //log it all to file
    }
}

奇怪的是,在一些较旧的日志文件中,该值被写为 10 而在较新的日志文件中,它被写为 true< /code> 或 false

所以发生了以下事情之一。原因是:

  1. 有人更改了代码
  2. 有人将表中的数据类型从 int -> 更改为 。 bit
  3. 有人将存储过程中的数据类型从 int 更改为 >位(也许那里有一个转换/转换被删除)
  4. 几个月前在我们所有的生产服务器上安装了 .NET 3.5,并且日期似乎与日志文件中的更改一致,

我已经在最后一个小时左右的时间里,我在谷歌上搜索了 .net 2.0 和 .net 2.0 之间的变化。 .net 3.5 但没有什么让我惊讶的。

任何人都可以阐明这一点吗?

I've inherited a piece of code at work and I've just noticed a fairly trivial but bizarre little quirk with it. So maybe someone can save me setting up clean .NET 2.0 Environment to test it out.

There is a SQL2K5 table containing a column called IsEnabled BIT NOT NULL

There is a sproc selecting from that table

There is a piece of C# code which, using a SQL Reader calls that SPROC, loops through the result rows, and writes some of that info to a log file as well as doing other things.

using (SqlDataReader reader = DAL.SqlHelper.ExecuteReader(connStr, "usp_MySproc"))
{
    while (reader.Read())
    {
         //code to do stuff...

         string s = reader["IsEnabled "].ToString(); //exactly like this...

         //code to concatenate `s` with other values
         //log it all to file
    }
}

The odd thing is, in some older log files, that value is written out as a 1 or a 0 whereas in newer logfiles it's written out as a true or a false

So one of the following things has happened. It was caused by:

  1. Someone changing the code
  2. Someone changing the datatype in the table from int -> bit
  3. Someone changing the datatype in the sproc from int -> bit (maybe there was a cast/convert in there that got removed)
  4. an installation of .NET 3.5 across all of our production servers a few months ago, and the dates do seem to coincide with the change in the log files,

I've trawled google for that last hour or so looking for changes between .net 2.0 & .net 3.5 but nothings jumping out at me.

Can anyone shed any light on this ?

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

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

发布评论

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

评论(1

饭团 2024-10-02 02:03:36

您可以为 .NET 2.0 重建代码并运行测试以查看您现在的代码库是否输出 1/0 或 true/false。这应该会给你答案。另外,在这种情况下您调用的并不是真正的 boolean.ToString() 。它是数据读取器,使用默认的收集和转换。当对位字段调用 to string 时,datareader 可能改变了它输出位字段的方式。

You could rebuild your code for .NET 2.0 and run a test to see if the code base you have now outputs 1/0 or true/false. That should provide you your answer. Also, it's not really boolean.ToString() that you're calling in this case. It is the data reader, using the default collection and casting. It may be that datareader changed how it outputs bit fields when to string is called on them.

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