在我的litte项目中,我有两个listview和两个db . 第一个listview是从第一个数据库表填充的 . 第二个是从第一个选定项目填充 .
这两个列表框以相同的模式构建:
`name | surname | id`.
在第一个列表框中,我有在db中插入的所有用户的列表
`Ale | Zucchelli | 5`
`Faby | Jake | 6`
`Alexa | Doctor | 7`
`Mattias | Rossi | 12`
当我选择一个元素并单击移动按钮时,选择项目将移动到第二个列表框 .
second listbox items
`Ale | Zucchelli | 5`
`Mattias | Rossi | 12`
当我点击Insert按钮时,第二个列表框的项目我必须将它发送到我的db的第二个表,但我必须只注册id .
我试过这段代码:
private void btmconferma_Click(object sender,EventArgs e){
string idutente;
//string idu_retrive = string.Empty;
//object value = 0;
idutente = user.Text;
//string querysql2 = " INSERT INTO AMICIZIA (IdUtente1, [idamici]) VALUES ('" + idutente + "', '" + value + "')";
//SqlConnection myconn = new SqlConnection(CnnStr);
//SqlCommand mycmd = new SqlCommand(querysql2, myconn);
foreach (ListViewItem l in listamicizie.Items)
{
string CnnStr = System.Configuration.ConfigurationSettings.AppSettings["CnnStr"].ToString();
SqlConnection myconn = new SqlConnection(CnnStr);
myconn.Open();
SqlCommand cmd = new SqlCommand("insert into AMICIZIA (IdUtente1) values('" + idutente + "')", myconn);
//SqlCommand cmd1 = new SqlCommand("insert into AMICIZIA (idamici) values('" + l.SubItems["IdUtente"].ToString() + "')", myconn);
SqlCommand cmd1 = new SqlCommand("insert into AMICIZIA (idamici) values('" + listamicizie.Columns["IdUtente"].Text + "')", myconn);
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();
myconn.Close();
}
this.Close();
}
IdUtente的值是正确的,但idamici的值始终为零 . 我怎么样?
1 回答
您的SQl应如下所示:
ListViewItem l
是您创建的循环中的实际值 .