我有5个复选框可能我必须选择一个或多个并保存到表中

时间:2016-04-20 05:58:41

标签: c# sql-server-2008-r2

这是我的ms sql server表

create table tblAdd_Subject ( 
S_ID int identity(100,1)primary key,
Subject1 varchar(50), 
Subject2 varchar(50), 
Subject3 varchar(50), 
Subject4 varchar(50), 
Subject5 varchar(50));

我有5个复选框可能我必须选择1个或多个复选框并单击保存按钮它是C#win form application

1 个答案:

答案 0 :(得分:1)

我猜你需要这样的东西。

    SqlConnection connection = new SqlConnection("[your connection string goes here]");
    connection.Open();

    SqlCommand cmd = new SqlCommand("INSERT INTO tblAdd_Subject(Subject1, Subject2, Subject3, Subject4, Subject5) values(@Subject1, @Subject2, @Subject3, @Subject4, @Subject5)", connection);

    cmd.Parameters.Clear();
    cmd.Parameters.AddWithValue("@Subject1", chk1.Checked ? "some value for subject 1" : string.Empty);
    cmd.Parameters.AddWithValue("@Subject2", chk2.Checked ? "some value for subject 2" : string.Empty);
    cmd.Parameters.AddWithValue("@Subject3", chk3.Checked ? "some value for subject 3" : string.Empty);
    cmd.Parameters.AddWithValue("@Subject4", chk4.Checked ? "some value for subject 4" : string.Empty);
    cmd.Parameters.AddWithValue("@Subject5", chk5.Checked ? "some value for subject 5" : string.Empty);

    cmd.ExecuteNonQuery();