删除无效 [英] Delete is not working

查看:131
本文介绍了删除无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码中没有错误消息,但不删除记录。如果逻辑上有任何错误,请指导我:代码如下:



我尝试过:



 private void button2_Click(object sender,EventArgs e)
{
con.Open();

OleDbCommand delcmd = new OleDbCommand();
// if(dataGridView1.Rows.Count> 1&& dataGridView1.SelectedRows [0] .Index!= dataGridView1.Rows.Count-1)
if(dataGridView1.SelectedRows.Count> ; 0&& dataGridView1.SelectedRows [0] .Index!= dataGridView1.Rows.Count-1)

{
delcmd.CommandText =从table123删除Idno =+ dataGridView1.SelectedRows [0] .Cells [0] .Value.ToString()+;

delcmd.Connection = con;
delcmd.ExecuteNonQuery();

dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows [0] .Index);
MessageBox.Show(Row Deleted);
bind();
clear();
}
else
{
Messagebox.show(无法删除);
}
con.close();
}

解决方案

首先,不要这样做!永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。



其次,如果没有删除任何内容,那是因为SQL的WHERE子句无法匹配。这可能是你有错误的列,错误的datagridview,或者数据不是你所期望的 - 我们无法分辨,因为我们没有运行您的应用程序,无论如何都无法访问您的数据库。 />
因此,使用调试器确切地找出传递给SQL的内容,然后手动查询数据库以查找任何匹配的行。如果不存在 - 而且他们不存在 - 你需要看看为什么。但我们不能为您做任何事情!


此视频将帮助您开始调试:使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

there is no error message in my code, but not delete the record. if there is any error in logic,please guide me:the code is below:

What I have tried:

private void button2_Click(object sender, EventArgs e)
        {
            con.Open();
            
            OleDbCommand delcmd = new OleDbCommand();
            //if (dataGridView1.Rows.Count > 1 && dataGridView1.SelectedRows[0].Index != dataGridView1.Rows.Count-1)
            if(dataGridView1.SelectedRows.Count > 0 && dataGridView1.SelectedRows[0].Index!=dataGridView1.Rows.Count-1)
            
            {
                delcmd.CommandText = "delete from table123 where Idno=" + dataGridView1.SelectedRows[0].Cells[0].Value.ToString() + "";
               
                delcmd.Connection = con;
                delcmd.ExecuteNonQuery();
             
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                MessageBox.Show("Row Deleted");
                bind();
                clear();
            }
else
{
Messagebox.show("can’t delete");
}
con.close();
                    }

解决方案

First off, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Second, if nothing gets deleted, it's because the WHERE clause of your SQL fails to match. That may be that you have the wrong column, the wrong datagridview, or that the data is not what you expect - we can't tell, because we don't have your app running and can't access your DB anyway.
So use the debugger to find out exactly what you are passing to SQL, and then manually query your DB to find any matching rows. If none exist - and they don't - you need to look at why. But we can't do any of that for you!


This video will help you get started with debugging: Basic Debugging with Visual Studio 2010 - YouTube[^]


这篇关于删除无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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