如何在C#中定义CheckBox

时间:2022-11-22 16:02:20

I have a check box in my form that I need to define along with other fields for inserting data. But I am not sure How to define it for inserting data. Below is my code.

我的表单中有一个复选框,我需要与其他字段一起定义以插入数据。但我不确定如何定义插入数据。以下是我的代码。

cmd.Parameters.AddWithValue("@EmailTmp", (txtEmail.Text));
cmd.Parameters.AddWithValue("@PhoneTmp", (txtPhone.Text));
cmd.Parameters.AddWithValue("@AgreeTmp", (cbAgree.CheckBox));

2 个解决方案

#1


2  

Looking at your pasted code, I think you want this:

看看你粘贴的代码,我想你想要这个:

cmd.Parameters.AddWithValue("@AgreeTmp", (cbAgree.Checked));

or possibly something like:

或者可能是这样的:

cmd.Parameters.AddWithValue("@AgreeTmp", (cbAgree.Checked ? 1 : 0));

#2


2  

cmd.Parameters.AddWithValue("@AgreeTmp", (cbAgree.Checked));

This will give you a boolean value of true if checked, false if not. SQL will translate this into a 0/1 if you have the field in the SQL db as a bit.

如果选中,这将给出布尔值true,否则返回false。如果您在SQL db中有一个字段,SQL会将其转换为0/1。

Edit: As a side note, you don't need the "@" in front of the AgreeTmp

编辑:作为旁注,您不需要在AgreeTmp前面的“@”

#1


2  

Looking at your pasted code, I think you want this:

看看你粘贴的代码,我想你想要这个:

cmd.Parameters.AddWithValue("@AgreeTmp", (cbAgree.Checked));

or possibly something like:

或者可能是这样的:

cmd.Parameters.AddWithValue("@AgreeTmp", (cbAgree.Checked ? 1 : 0));

#2


2  

cmd.Parameters.AddWithValue("@AgreeTmp", (cbAgree.Checked));

This will give you a boolean value of true if checked, false if not. SQL will translate this into a 0/1 if you have the field in the SQL db as a bit.

如果选中,这将给出布尔值true,否则返回false。如果您在SQL db中有一个字段,SQL会将其转换为0/1。

Edit: As a side note, you don't need the "@" in front of the AgreeTmp

编辑:作为旁注,您不需要在AgreeTmp前面的“@”