如何设置复选框的值? [英] How do I set a value for a checkbox?
问题描述
我有一个连接到sql服务器数据库的Web窗体应用程序.在我的.net解决方案中,我有一个业务对象类,该类具有以下复选框字段,这些复选框字段定义为布尔数据类型,并且其默认值设置为false.以下示例:
公共 class AccountTracker
{
公共 布尔 ModemRequirement { get ; 设置; }
公共 布尔 ModemConnectionToNetworkDevice { get ; 设置; }
公共 布尔 PasswordProtected { get ; 设置; }
}
公共 AccountTracker()
{ModemRequirement = false ;
ModemConnection = false ;
PasswordProtected = false ;
</ pre >
这在我的.net解决方案中可以正常工作,但是在我的sql server数据库中,这些相同字段的数据类型有点(0或1).
我希望有人可以发布一些伪代码,以显示如何从布尔转换为位.这是我到目前为止尝试过的事情之一.
如果(modemrequirement.Checked = true )
{
modemrequirement = 1 ;
}
其他
{modemrequirement.Checked!= true )
{
modemrequirment = 0
}
关于如何正确解决此问题的任何想法或建议?.NET中的
bool
/Boolean
和SQL中的bit
是同一类型.无需转换.
当您从SQL读取bit
字段时,它已经是bool
;将该值强制转换为bool
或使用Field<bool>
扩展方法:
bool theValue =( bool )reader [ YourBitColumn"]; bool theValue = reader.Field< bool>(" );
当您将bool
作为bit
参数传递给查询时,只需将其传递给:
bool theValue = true; command.Parameters.AddWithValue("@YourBitParameter", theValue);
I have a web forms application that is connected to a sql server database. In my .net solution I have a Business Object Class that has the following checkbox fields defined as boolean data types and their default value is being set to false. Example Below:
public class AccountTracker
{
public bool ModemRequirement { get; set; }
public bool ModemConnectionToNetworkDevice { get; set; }
public bool PasswordProtected { get; set; }
}
public AccountTracker()
{ModemRequirement =false;
ModemConnection = false;
PasswordProtected=false;
</pre>
this works fine in my .net solution but in my sql server database the datatype for those same fields is a bit (0 or 1).
I''m hoping that someone can post some psuedocode that will show how to convert from bool to bit. Here is one of the things I''ve tried so far.
if (modemrequirement.Checked = true)
{
modemrequirement = 1;
}
else
{modemrequirement.Checked != true)
{
modemrequirment = 0
}
Any ideas or suggestion on how to properly approach this issue?
bool
/Boolean
in .NET andbit
in SQL are the same type. There is no conversion required.
When you''re reading abit
field from SQL, it''s already abool
; either cast the value tobool
, or use theField<bool>
extension method:
bool theValue = (bool)reader["YourBitColumn"]; bool theValue = reader.Field<bool>("YourBitColumn");
When you''re passing abool
as abit
parameter to a query, just pass it in:
bool theValue = true; command.Parameters.AddWithValue("@YourBitParameter", theValue);
这篇关于如何设置复选框的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!