如何阅读是/否值从Access数据库的布尔?布尔、数据库、Access

2023-09-04 01:27:39 作者:花开若相依

帮我找回从布尔格式MS访问 YES / NO 数据类型。

Help me to retrieve the YES/NO Datatype from MS access in Boolean format.

我试图解析它,但它始终返回false。

I tried parsing it but it always returned false.

更新:没有问题,实际上 对不起,它确实接受YES / NO为布尔值。

UPDATE: Was not problem actually Sorry, it does accepts YES/NO as boolean values.

OleDbconnection dbConnect = new OleDbConnection(".....*.MDB");
dbConnect.Open();
.....
...
//xyz = dbCommand.ExecuteReader()
bool value = (bool)xyz[1];

下一次,我会更多的研究,并要求前发现小错误..对不起的人

Next time I'll research more and find minor mistakes before asking.. Sorry people

推荐答案

随着最后把这个问题休息了希望:

With the hope of finally putting this question to rest:

据没关系的Access数据库引擎使用什么样的内部重新$ P $的psentations 是/真实否/假。我们得到一个可选System.Boolean 值。

It does not matter what the Access Database Engine uses for its internal representations of Yes/True and No/False. We get back a System.Boolean value.

在Access中,是/否字段或者是是/真实无/假 NULL 否/假

In Access, Yes/No fields are either Yes/True or No/False. NULL values are No/False.

测试数据:

测试code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.OleDb;

namespace oleDbTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string myConnectionString;
            myConnectionString =
                    @"Provider=Microsoft.ACE.OLEDB.12.0;" +
                    @"Data Source=C:\Users\Public\Database1.accdb;";

            using (var con = new OleDbConnection())
            {
                con.ConnectionString = myConnectionString;
                con.Open();

                using (var cmd = new OleDbCommand())
                {
                    // just to be sure, let's force one of the values to Null
                    cmd.Connection = con;
                    cmd.CommandText =
                            "UPDATE YesNoTable SET YesNoField = NULL " +
                            "WHERE Description = 'Null'";
                    cmd.ExecuteNonQuery();
                }

                using (var cmd = new OleDbCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandText =
                            "SELECT ID, YesNoField, Description FROM YesNoTable";
                    OleDbDataReader rdr = cmd.ExecuteReader();
                    while (rdr.Read())
                    {
                        Console.WriteLine(String.Format("Row {0}:", rdr["ID"]));
                        bool boolValue = Convert.ToBoolean(rdr["YesNoField"]);
                        Console.WriteLine(String.Format("    Description is: {0}", rdr["Description"]));
                        Console.WriteLine(String.Format("    Return type is: {0}", rdr["YesNoField"].GetType()));
                        Console.WriteLine(String.Format("    raw value is: {0}", rdr["YesNoField"]));
                        Console.WriteLine(String.Format("    boolValue is: {0}", boolValue));
                        Console.WriteLine();
                    }
                }
                con.Close();
            }
            Console.WriteLine("Done.");
        }
    }
}

结果:

Row 1:
    Description is: Yes
    Return type is: System.Boolean
    raw value is: True
    boolValue is: True

Row 2:
    Description is: No
    Return type is: System.Boolean
    raw value is: False
    boolValue is: False

Row 3:
    Description is: Null
    Return type is: System.Boolean
    raw value is: False
    boolValue is: False

Done.