从数据库读取布尔值?

发布于 2024-11-06 04:56:25 字数 723 浏览 6 评论 0原文

在C#中,使用SqlDataReader,有没有办法从数据库读取布尔值?

while (reader.Read())
{
    destPath = reader["destination_path"].ToString();
    destFile = reader["destination_file"].ToString();
    createDir = reader["create_directory"].ToString();
    deleteExisting = Convert.ToBoolean(reader["delete_existing"]);
    skipIFolderDate = reader["skipifolderdate"].ToString();
    useTempFile = reader["useTempFile"].ToString();
    password = reader["password"].ToString();
}

在上面的代码中,delete_existing 在数据库中始终为 1 或 0。我在 MSDN 上读到 Convert.ToBoolean() 不接受 1 或 0 作为有效输入。它只接受 true 或 false。是否有其他方法将 DB 值转换为布尔值?或者我需要在 SqlDataReader 之外执行此操作吗?

另外,我无法更改 DB 值,因此请不要回答“将 DB 值从 1 和 0 更改为 true 和 false”。

谢谢!

In C#, using SqlDataReader, is there a way to read a boolean value from the DB?

while (reader.Read())
{
    destPath = reader["destination_path"].ToString();
    destFile = reader["destination_file"].ToString();
    createDir = reader["create_directory"].ToString();
    deleteExisting = Convert.ToBoolean(reader["delete_existing"]);
    skipIFolderDate = reader["skipifolderdate"].ToString();
    useTempFile = reader["useTempFile"].ToString();
    password = reader["password"].ToString();
}

In the code above, delete_existing is always a 1 or 0 in the DB. I read on MSDN that Convert.ToBoolean() does not accept a 1 or 0 as valid input. It only accepts true or false. Is there an alternative way to convert a DB value to a bool? Or do I need to do this outside of the SqlDataReader?

Also, I can not change the DB values, so please no answers saying, "Change the DB values from 1's and 0's to true's and false's."

Thanks!

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

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

发布评论

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

评论(5

傲世九天 2024-11-13 04:56:25

如果delete_existing的类型是sqlserver'bit'类型,你可以这样做:

var i = reader.GetOrdinal("delete_existing"); // Get the field position
deleteExisting = reader.GetBoolean(i);

或者(但如果delete_existing可以是DBNull,它会崩溃)

deleteExisting = (bool)reader["delete_existing"];

或者更好,下面的这个是 DBNull 证明,如果列是 DBNull,则返回 false

deleteExisting = reader["delete_existing"] as bool? ?? false;

否则,如果数据库类型是int

deleteExisting = (reader["delete_existing"] as int? == 1) ? true : false;

或者如果它是 varchar

deleteExisting = (reader["delete_existing"] as string == "1") ? true : false;

If the type of delete_existing is a sqlserver 'bit' type, you can do :

var i = reader.GetOrdinal("delete_existing"); // Get the field position
deleteExisting = reader.GetBoolean(i);

or (but it will crash if delete_existing can be DBNull)

deleteExisting = (bool)reader["delete_existing"];

or better, this onebelow is DBNull proof and returns false if the column is DBNull

deleteExisting = reader["delete_existing"] as bool? ?? false;

Otherwise if the database type is int :

deleteExisting = (reader["delete_existing"] as int? == 1) ? true : false;

or if it is a varchar

deleteExisting = (reader["delete_existing"] as string == "1") ? true : false;
放飞的风筝 2024-11-13 04:56:25

铸造作品:
myVar = (bool)dataReader["myColumn"];

Casting works:
myVar = (bool)dataReader["myColumn"];

怎樣才叫好 2024-11-13 04:56:25

这个怎么样?

deleteExisting = (reader["delete_existing"] as int?) == 1;

布尔值可能是最容易转换的类型。这是“Y”、“N”版本:

deleteExisting = string.Equals(reader["delete_existing"] as string, "Y", StringComparision.OrdinalIgnoreCase);

How about this?

deleteExisting = (reader["delete_existing"] as int?) == 1;

Boolean is probably the easist type to convert something to. Here's the 'Y', 'N' version:

deleteExisting = string.Equals(reader["delete_existing"] as string, "Y", StringComparision.OrdinalIgnoreCase);
乖乖 2024-11-13 04:56:25

如果您在 SELECT 中使用 CASE 并希望使用 GetBoolean,则在读取之前使用 CAST 将列更改为位。

例如:

SELECT CAST((CASE WHEN [Condition] THEN 1 ELSE 0 END) as bit) FROM Table_Name

那么你可以使用

reader.GetBoolean(0)

If you are using CASE in SELECT and want to use GetBoolean then use CAST to change the column to bit before reading.

For eg:

SELECT CAST((CASE WHEN [Condition] THEN 1 ELSE 0 END) as bit) FROM Table_Name

then you can use

reader.GetBoolean(0)
听不够的曲调 2024-11-13 04:56:25
deleteExisting = reader.GetBoolean(reader["delete_existing"]);
deleteExisting = reader.GetBoolean(reader["delete_existing"]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文