向表中添加记录问题
窗体中有两个listview控件,分别显示不同记录,当选择listview1中某条记录,点击button按钮时,添加到listview2中的表中去并显示,listview2中的第一列显示是用窗体中标签的内容显示的,代码如下,但是老是执行错误,请问我哪里出现了问题啊?! 谢谢!
private void button1_Click(object sender, EventArgs e)
{
this.Add_Xfxm();
this.UpdateListview();//updatelistview是我自定义的函数,用于显示记录
}
private void Add_Xfxm()
{
int count = this.listView1.SelectedItems.Count;
if (count == 0)
{
MessageBox.Show("请选择记录!", "选择记 录",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
int s = Convert.ToInt32(this.textBox2.Text.ToString());
foreach (int i in this.listView1.SelectedIndices)
{
if (this.listView1.Items[i].Selected == true)
{
string j = this.listView1.Items[i].SubItems[1].Text.ToString();
string z = this.listView1.Items[i].SubItems[2].Text.ToString();
string b = this.listView1.Items[i].SubItems[3].Text.ToString();
}
this.selectStr3 = " INSERT INTO Xfxm ( Ctid,Xfxm,Dj,Sl,Bz) values ('this.label10.Text.ToString ()','j','z','s','b')";
}
this.sqlcommand3.CommandText = this.selectStr3;
this.conn3.Open();
this.sqlcommand3.ExecuteNonQuery();
}
------解决方案--------------------
" INSERT INTO Xfxm ( Ctid,Xfxm,Dj,Sl,Bz) values ('this.label10.Text.ToString ()','j','z','s','b')";
变量可以直接这样写吗?
应该是'"+this.label10.Text.ToString ()+"','"+j+"','"+z+"','"+s+"','"+b+"'吧!
还有你的连接没有关闭
在 this.sqlcommand3.ExecuteNonQuery(); 下面
conn3.close();