关于C#winform平台listview控件删除数据

zyf1097727546 2017-07-26 04:36:15
private void 删除文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
if (filesList.SelectedItems.Count != 0)
{
DialogResult dlgResult;
dlgResult = MessageBox.Show("确定要删除该组?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
if (dlgResult == DialogResult.Yes)
{
string conn = "Data Source=(local);Initial Catalog=edg;Integrated Security=True";
SqlConnection connection = new SqlConnection(conn);
connection.Open();
string select = this.filesList.SelectedItems[0].Text.ToString();
string sql ="delete from 科普活动 where ID="+"'"+select+"'";
SqlCommand cmd = new SqlCommand(sql, connection);
filesList.SelectedItems[0].Remove();
MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
//MessageBox.Show(listView1.SelectedItems[0].Text);
}
}
else
MessageBox.Show("请先选择要删除的组名!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
为什么我的代码只能删除控件里的数据,数据库里的数据没有删掉.....sql语句调试后复制到数据库也没问题....
...全文
286 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
shoppo0505 2017-07-26
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
SqlCommand cmd = new SqlCommand(sql, connection); -》 SqlCommand cmd = new SqlCommand(sql, connection); cmd.ExecuteNonQuery(); cmd 没有执行。
+1
exception92 2017-07-26
  • 打赏
  • 举报
回复
其实最应该这样:
int result = cmd.ExecuteNonQuery();
if(result>0)
{
    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
else
{
   // 删除失败提示
}
shoppo0505 2017-07-26
  • 打赏
  • 举报
回复
你看一下 string select = this.filesList.SelectedItems[0].Text.ToString(); 这里的select值, 然后结合 "delete from 科普活动 where ID="+"'"+select+"'"; 看看能不能实际删除。 初步怀疑是select 的值,不符合ID的值
exception92 2017-07-26
  • 打赏
  • 举报
回复
SqlCommand cmd = new SqlCommand(sql, connection); -》 SqlCommand cmd = new SqlCommand(sql, connection); cmd.ExecuteNonQuery(); cmd 没有执行。

110,534

社区成员

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

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

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