SQL 查询从 BIT 类型列获取布尔值

发布于 2024-12-22 20:29:21 字数 1345 浏览 3 评论 0原文

我在从数据库的之一获取boolean值时遇到问题。我正在使用 SQL Server 2008,其中我创建了一个数据库,如下所示:

表名称:SysUser3 列为:

ProductName ||产品ID || SelectedProducts

SelectedProducts 列是一个BIT 类型列,包含当前每个行条目的False 值。

现在,我正在编写一个 SQL 查询来从“SelectedProducts”列中获取布尔值,

这是我的代码:

    using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database.mdf;User Instance=true"))
        {
            con.Open();

            string cmdString = "SELECT ProductName,SelectedProducts FROM SysUser3";
            using (SqlCommand cmd = new SqlCommand(cmdString, con))
            {
                using (SqlDataReader dataRead = cmd.ExecuteReader())
                {
                    while (dataRead.Read())
                    {
                        items.Add(new ProductModel
                        {
                            Selected=(bool)dataRead["SelectedProducts"];
                            ProductName= dataRead["ProductName"].ToString()
                        });
                    }
                }
            }
        }

我在这一行收到错误,因此无法运行代码:

Selected=(bool)dataRead["SelectedProducts"];

我做得正确吗?有人可以告诉我代码有什么问题吗?

I am facing a problem getting the boolean value from one of the columns of my database. I am using SQL Server 2008 where in I have created a databaseas follows:

Table name: SysUser3 and columns as:

ProductName || ProductId || SelectedProducts

The column SelectedProducts is a BIT type column and contains False values for each of the row entries at present.

Now, I am writing a SQL Query to get the boolean value from my 'SelectedProducts' column

Here is my code:

    using (SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Database.mdf;User Instance=true"))
        {
            con.Open();

            string cmdString = "SELECT ProductName,SelectedProducts FROM SysUser3";
            using (SqlCommand cmd = new SqlCommand(cmdString, con))
            {
                using (SqlDataReader dataRead = cmd.ExecuteReader())
                {
                    while (dataRead.Read())
                    {
                        items.Add(new ProductModel
                        {
                            Selected=(bool)dataRead["SelectedProducts"];
                            ProductName= dataRead["ProductName"].ToString()
                        });
                    }
                }
            }
        }

I am getting an error at this line and hence not able to run the code:

Selected=(bool)dataRead["SelectedProducts"];

Am I doing it correctly ? can someone tell me what's wrong in the code?

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

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

发布评论

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

评论(2

北斗星光 2024-12-29 20:29:21

你有一个分号放错地方了。将其更改为逗号。
它应该是:

                while (dataRead.Read())
                {
                    items.Add(new ProductModel()
                    {
                        Selected=(bool)dataRead["SelectedProducts"],
                        ProductName= dataRead["ProductName"].ToString()
                    });
                }

You have a semicolon misplaced. Change it to a comma.
It should read:

                while (dataRead.Read())
                {
                    items.Add(new ProductModel()
                    {
                        Selected=(bool)dataRead["SelectedProducts"],
                        ProductName= dataRead["ProductName"].ToString()
                    });
                }
你的往事 2024-12-29 20:29:21

您可以尝试GetBoolean(column_odrinal)方法。

  if(!dataRead.IsDBNull(1))
     Selected=dataRead.GetBoolean(1);

或者如果返回值为空,您可以修复它。

Selected =  (dataRead["SelectedProducts"] as bool?) ??  false ;

You may try GetBoolean(column_odrinal) method.

  if(!dataRead.IsDBNull(1))
     Selected=dataRead.GetBoolean(1);

OR you may fix it if return value if null.

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