【发布时间】:2017-04-12 17:15:51
【问题描述】:
我正在尝试使用 Visual Studio 2013 和 SQL Server 2014 开发 Windows 窗体应用程序。
我的问题是关于获取检查的复选框的值,并将所有数据保存到 SQL Server 中的一行。
例子:
TBL_EXAMPLE:
ID int PK not null,
NAME nvarchar(max) null
我的程序中有 3 个复选框:
checkbox1 - name : chkbx1 checked
checkbox2 - name : chkbx2 checked
checkbox3 - name : chkbx3 not checked
我需要将复选框的值添加到 SQL Server 中名为 name 的一列
添加SQL Server后NAME的内容应该是这样的:
NAME
checkbox1 checkbox2
我试着这样写:
foreach (Control cont in this.groupBox1.Controls)
{
if (cont is CheckBox)
{
if (((CheckBox)cont).Checked == true)
{
sqlcon.Open();
SqlCommand sqlcmd = new SqlCommand("INSERT INTO TBL_EXAMPLE VALUES ( '" + ((CheckBox)cont).Text.ToString() + "')", sqlcon);
sqlcmd.ExecuteNonQuery();
sqlcon.Close();
MessageBox.Show("OK");
}
}
}
它给出了这样的结果:
ID NAME
1 checkbox1
2 checkbox2
我想看看这个:
ID NAME
1 checkbox1 checkbox2
请指教。
【问题讨论】:
标签: c# sql sql-server checkbox groupbox