简体   繁体   English

如何使用C#将复选框值存储到SQL Server数据库中

[英]How to store a checkbox value into sql server database using c#

I'm working on "Time Table Scheduling using genetic algorithm" project using C# and SQL Server.. 我正在使用C#和SQL Server进行“使用遗传算法进行时间表调度”项目。

I divided the project into 3 layers data access layer, business layer, and interfaces. 我将项目分为3层:数据访问层,业务层和接口。

Data access layer contains: 数据访问层包含:

  • constructor to initialize the connection object 构造函数,用于初始化连接对象
  • method to open the connection 打开连接的方法
  • method to read data from database 从数据库读取数据的方法
  • method to insert, update, and delete data from database 从数据库插入,更新和删除数据的方法

For example: 例如:

//Method to insert , update ,delete data from database
public void ExecuteCommand(string stored_procedure, SqlParameter[] param)
{
            SqlCommand sqlcmd = new SqlCommand();
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.CommandText = stored_procedure;
            sqlcmd.Connection = sqlconnection;

            if (param != null)
            {
                sqlcmd.Parameters.AddRange(param);
            }
            sqlcmd.ExecuteNonQuery();
}

The business layer contains class for each form 业务层包含每种表单的类

For example: ADD_COURSE.CLS for "Add COURSE" 例如:“添加课程”的ADD_COURSE.CLS

class CLS_ADD_COURSE
{
        public void ADD_COURSE(string DESC_COURSE, int TERM, bool LAB, bool Specialized)
        {
            DAL.DataAccessLayer DAL = new DAL.DataAccessLayer();
            DAL.Open();
            SqlParameter[] param = new SqlParameter[4];

            param[0] = new SqlParameter("@DESC_COURSE", SqlDbType.VarChar, 50);
            param[0].Value = DESC_COURSE;

            param[1] = new SqlParameter("@TERM", SqlDbType.Int);
            param[1].Value = TERM;

            param[2] = new SqlParameter("@LAB", SqlDbType.Bit);
            param[2].Value = LAB;

            param[3] = new SqlParameter("@Specialized", SqlDbType.Bit);
            param[3].Value = Specialized;

            DAL.ExecuteCommand("ADD_COURSE", param);
            DAL.Close();
}

Now I want to store the data into the database so I wrote this instruction in form but it doesn't work for checkbox value 现在,我想将数据存储到数据库中,所以我以表格形式编写了此指令,但不适用于复选框值

COURSE.ADD_COURSE(txt_COURSE_name.Text, Convert.ToInt32(TXT_COURSE_TERM.Text),Convert.ToBoolean(CHECK_LAB.Text),Convert.ToBoolean(CHECK_SP.Text));

So how to store a checkbox value into a SQL Server database using C#? 那么如何使用C#将复选框值存储到SQL Server数据库中呢?

This my procedure: 这是我的程序:

create PROC [dbo].[ADD_COURSE]
   @DESC_COURSE VARCHAR(50),
   @TERM INT,
   @LAB BIT,
   @Specialized BIT
AS
   INSERT INTO [dbo].[COURSE]([DESC_COURSE], [TERM], [LAB],[Specialized ])  
   VALUES (@DESC_COURSE, @TERM, @LAB, @Specialized)

You can use int,tinyint, bit to store it in database. 您可以使用int,tinyint, bit将其存储在数据库中。 If the the checkbox is checked store it as 1, if it is unchecked store it is as 0. When you take the value of the checkbox you should perform same thing. 如果该复选框已选中 ,则将其存储为1;如果未选中 ,则将其存储为0。采用该复选框的值时,应执行相同的操作。 If the value in the database is 1 set the checkbox to be checked. 如果数据库中的值为1,则选中该复选框。

There is possibility to store multiple values of checkbox in one database field using bitmask logic. 可以使用位掩码逻辑在一个数据库字段中存储复选框的多个值。 But probably you don't need that in your case. 但是可能您不需要这种情况。

you can do something like this. 你可以做这样的事情。 here i am inserting employee into db.. see admin rights. 在这里,我将员工插入数据库。。请参阅管理员权限。 which is a check box and adminrights db type is bit 这是一个复选框,adminrights数据库类型为bit

                SqlCommand Command = new SqlCommand();
                Command.Connection = MyConnection;
                Command.CommandText =
                    "Insert into Employees (ID, Name, Last_name, Username, Password, E_mail, Address, administrator_rights)"
                    + "values(@ID, @Name, @Last_name, @Username, @Password, @E_mail,Address, @admin_rights )";

            Command.Parameters.Add("@ID", 1); // some generated number
            Command.Parameters.Add("@Name", TextBoxName.Text);
            Command.Parameters.Add("@Last_name", TextBoxLastName.Text);
            Command.Parameters.Add("@Username", TextBoxUserName.Text);
            Command.Parameters.Add("@Password", TextBoxPassword.Text);
            Command.Parameters.Add("@E_mail", TextBoxEmail.Text);
            Command.Parameters.Add("@Address", TextBoxAddress.Text);
            Command.Parameters.Add("@admin_rights", CheckBoxAdminRights.Checked);

            using (MyConnection)
            {
                Command.ExecuteNonQuery();
            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM