如何从多个datagridviews中删除记录 [英] How to delete records from multiple datagridviews

查看:73
本文介绍了如何从多个datagridviews中删除记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我点击一个按钮时,我想从两个网格视图中删除数据(使用外键)!这是我尝试过的并且给我一个错误INNER附近的语法错误。帮我看看这个out。



我尝试了什么:



 私有  Sub  Button1_Click( Byval  ..... 
Dim 命令 as new SqlCommand( 从学生中删除Inner Join Classes ON Students.StudentID = Classes.StudentID Where Students。 StudentID ='& Registered.CurrentRow.Cells( 1 )。值& ',连接)

解决方案

也许你需要别名查询,然后做因为它需要知道你要删除的内容。您的查询看起来像这样,

   -   从表,s和c中删除。 
- 另外,请将别名替换为其他查询正确
DELETE s,c
FROM 学生s
INNER JOIN 类c ON Students.StudentID = Classes.StudentID
WHERE Students.StudentID = 12345

这会告诉SQL解析器实际解析查询并知道要删除的内容以及如何删除。



注意:我没有检查查询的其余部分是否有别名,所以请自行检查。但是别名会解决问题。


不是你问题的解决方案,而是你遇到的另一个问题。

永远不要通过连接字符串来构建SQL查询。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。

名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

I want to Delete data from two gridviews(Using Foreign Key) when i click a button!..This is What I've tried and gives me an error "Incorrect Syntax near "INNER""..Help Me figure this out.

What I have tried:

Private Sub Button1_Click(Byval.....
Dim command as new SqlCommand("Delete From Students Inner Join Classes ON Students.StudentID=Classes.StudentID Where Students.StudentID='" & Registered.CurrentRow.Cells(1).Value & "'",connection)

解决方案

Maybe you need to alias the query, and then do that because it needs to know what you are deleting. Your query would look something like this,

-- DELETE from both tables, s and c.
-- Also, please alias the rest of the query properly
DELETE s, c
FROM Students s
INNER JOIN Classes c ON Students.StudentID = Classes.StudentID 
WHERE Students.StudentID = 12345

This would tell the SQL parser to actually parse the query and know what to delete from and how to.

Note: I did not check the rest of the query for aliasing, so please check that on your own end. But aliasing would solve the problem.


Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]


这篇关于如何从多个datagridviews中删除记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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