帮助,借用系统 [英] Help, borrowing system

查看:84
本文介绍了帮助,借用系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我是编程中的新人请跟我一起承担

我有借阅系统,我有一个项目清单。如何借用它后退还物品??



我使用vb 2015与sql 2008作为其数据库



迫切需要你的帮助。谢谢你们



我的尝试:



 使用 cmd 
.Connection = ProjectConnect
.CommandText = UPDATE tblItems SET Quantity = @quan + 1 WHERE ItemID =& txtID.Text
.Parameters.AddWithValue( @ quan,txtQuan.Text)
.ExecuteNonQuery()
结束 使用



尝试这样做但是还不够,我需要在gridview中删除整个记录

解决方案

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



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood '   

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   TABLE  MyTable; 

完全有效的删除表格通讯和

   -   ' 

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你定期做备份,不是吗?而且我知道你知道参数是什么,因为你正在使用它们的数量。



但即便如此,那是奇怪的代码。你在设定数量吗(大概是库存数量)到项目数加一?如果他借用了你所拥有的十件物品中的一件,然后将其退回,那么那件机器人数将是2件 - 其他物品会发生什么变化?

可能你需要做的是

 ...  SET 数量=数量+  @ quan   WHERE  ... 


。CommandText =   UPDATE tblItems SET Quantity = @quan + 1 WHERE ItemID =& txtID.Text 



为什么要混合参数和连接?

这很危险。



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

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

SQL注入 - 维基百科 [ ^ ]

SQL注入 [ ^ ]

按示例进行SQL注入攻击 [ ^ ]

PHP:SQL注入 - 手册 [ ^ ]

SQL注入预防备忘单 - OWASP [ ^ ]


hi guys im a newb in programming pls bear with me
i have a borrowing system, i have a list of items. how do i return the item after borrowing it??

im using vb 2015 with sql 2008 as its database

urgently needs your help. thanks guys

What I have tried:

With cmd
            .Connection = ProjectConnect
            .CommandText = "UPDATE tblItems SET Quantity = @quan + 1  WHERE ItemID = " & txtID.Text
            .Parameters.AddWithValue("@quan", txtQuan.Text)
            .ExecuteNonQuery()
End With


tried doing this but its not enough, i need the whole record to be deleted in the gridview

解决方案

For starters, not 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.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you? And I know you know what parameters are, because you are using them for the quantity.

But even then, that's odd code.Whey are you setting the quantity (presumably the number in stock) to the number of items plus one? If he borrows one item of the ten you have in stock, and returns it then the instock count will be 2 - what happens to the other items?
Possibly what yo need to do is

... SET Quantity = Quantity + @quan WHERE ...


.CommandText = "UPDATE tblItems SET Quantity = @quan + 1 WHERE ItemID = " & txtID.Text


Why are you mixing parameters and concatenation?
It is dangerous.

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[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]


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

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