正在尝试删除列表框所选项目,但没有得到 [英] am trying to delete listbox selected item it is not getting

查看:82
本文介绍了正在尝试删除列表框所选项目,但没有得到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

----------------------

this is my code

----------------------

private void btnDelete_Click(object sender, EventArgs e)
       {
           SQLiteConnection connection = new SQLiteConnection(connectionString);

             delcmd.CommandText = "DELETE FROM Table WHERE ID='" + Listbox1.ToString() +"'";
               connection.Open();
               delcmd.Connection = connection;
               delcmd.ExecuteNonQuery();
               connection.Close();
               bindListbox1();
               Listbox1.Update();
               MessageBox.Show("Row Deleted");

                  }

推荐答案

Listbox1.ToString()不会给您您所需要的..

尝试使用Listbox1.SelectedValue

Listbox1.ToString() wont give you what you need..

try with Listbox1.SelectedValue

private void btnDelete_Click(object sender, EventArgs e)
{
   SQLiteConnection connection = new SQLiteConnection(connectionString); 
   SQLiteCommand delcmd = new SQLiteCommand(); // did you miss this?
   delcmd.CommandText = "DELETE FROM Table WHERE ID='" + Listbox1.SelectedValue +"'";
   connection.Open();
   delcmd.Connection = connection;
   delcmd.ExecuteNonQuery();
   connection.Close();
   bindListbox1();
   Listbox1.Update();
   MessageBox.Show("Row Deleted");

}


delcmd.CommandText = "DELETE FROM Table WHERE ID='" + Listbox1.ToString() +"'";



  1. 在SQL中使用正确的参数化命令.以这种方式连接字符串可能会使您遭受SQL注入攻击以及数据库的丢失或损坏.
  2. 为什么要尝试转换整个

  1. Use proper parameterised commands in SQL. Concatenating strings in this way can expose you to SQL injection attacks and the loss or corruption of your database.
  2. Why are you trying to convert the entire listbox[^] to a string and pass it into your command?



[edit]
修复了错误的链接.
[/edit]



[edit]
Fixed bad link.
[/edit]


这篇关于正在尝试删除列表框所选项目,但没有得到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆