SQL 查询从 BIT 类型列获取布尔值
我在从数据库的列
之一获取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 database
as 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你有一个分号放错地方了。将其更改为逗号。
它应该是:
You have a semicolon misplaced. Change it to a comma.
It should read:
您可以尝试
GetBoolean(column_odrinal)
方法。或者如果返回值为空,您可以修复它。
You may try
GetBoolean(column_odrinal)
method.OR you may fix it if return value if null.