求助!!checkedlistbox问题

jys2012 2013-01-15 12:42:42
for (int d = 0; d < checkedListBox1.Items.Count; d++)
{
if (checkedListBox1.GetItemChecked(d))
{
string st = this.checkedListBox1.Items.ToString();
string str3 = "insert into bingli(哮喘症状) values('" + st + "') ";
SqlCommand com3 = new SqlCommand(str3, con);
com3.ExecuteNonQuery();
}
}

要将选中的checkedlistbox的内容存入数据库,但是以上代码运行有误,求查看。。。。
数据库连接已经写在前面了。。
...全文
285 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
string st = this.checkedListBox1.Items.ToString();是毛? string st = this.checkedListBox1.Items[i].ToString();
jys2012 2013-01-15
  • 打赏
  • 举报
回复
用的是c#2008,SQL2005
jys2012 2013-01-15
  • 打赏
  • 举报
回复
额,没看到。。。 可以了
  • 打赏
  • 举报
回复
string st1 = this.checkedListBox2.CheckedItems[c].ToString(); => string st1 = this.checkedListBox2.Items[c].ToString();
jys2012 2013-01-15
  • 打赏
  • 举报
回复
改用update了。。还有一个问题,2段一样的语句,但是下一段有误:索引超出了数组界限!!求解? for (int d = 0; d < checkedListBox1.Items.Count; d++) { if (checkedListBox1.GetItemChecked(d)) { string st = this.checkedListBox1.Items[d].ToString(); string str3 = "update bingli set 哮喘症状='"+ st +"' where 病人编号 =" + Convert.ToInt32(textBox8.Text ) + ""; SqlCommand com3 = new SqlCommand(str3, con); com3.ExecuteNonQuery(); } } for (int c = 0; c < checkedListBox2.Items.Count; c++) { if (checkedListBox2.GetItemChecked(c)) { string st1 = this.checkedListBox2.CheckedItems[c].ToString(); string str4 = "update bingli set 鼻炎='" + st1 + "' where 病人编号 =" + Convert.ToInt32(textBox8.Text) + " "; SqlCommand com4 = new SqlCommand(str4, con); com4.ExecuteNonQuery(); } }
jys2012 2013-01-15
  • 打赏
  • 举报
回复
我在前面的语句中有写病人编号和其他的值,这里再赋值就重复了,
zfjclark 2013-01-15
  • 打赏
  • 举报
回复
病人编号这一列不能为空?那就赋个默认值啊
allencaosnfq 2013-01-15
  • 打赏
  • 举报
回复
Items - -! 的知道是那個 Items 。。!
jys2012 2013-01-15
  • 打赏
  • 举报
回复
出现错误:不能将值 NULL 插入列 '病人编号',表 'xc.dbo.bingli';列不允许有空值。INSERT 失败。
hard_learner 2013-01-15
  • 打赏
  • 举报
回复
Connection对象Open了?报什么错?直接把sql语句在查询分析器里面执行能成功?

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧