- Learn T-SQL Querying
- Pedro Lopes Pam Lahoud
- 318字
- 2021-06-24 14:38:14
Security
One reason to use parameterized queries is for security. Using a properly formatted parameterized query can protect against SQL injection attacks. A SQL injection attack is where a malicious user can execute database code (in this case, T-SQL) on a server by appending it to a data-entry field in the application. As an example, imagine we have an application that contains a form that asks the user to enter their name into a text box. If the application were to use an ad hoc statement to insert this data into the database, it would generally concatenate a T-SQL string with the user input, as in the following code:
DECLARE @sql nvarchar(MAX);
SET @sql = N'INSERT Users (Name) VALUES (''' + <user input> + ''');';
EXECUTE (@sql);
A malicious user might enter the Bob'); DROP TABLE Users; -- value into the text box.
If this is the case, the actual code that gets sent to SQL Server would look like the following:
INSERT Users (Name) VALUES ('Bob'); DROP TABLE Users; --');
This is valid T-SQL syntax that would successfully execute. It would first insert a row into the Users table with the Name column set to 'Bob', then it would drop the Users table. This would of course break the application, and unless there was some sort of auditing in place, we would never know what happened.
Let's look at this example again using a parameterized query. The code might look like the following:
EXECUTE sp_executesql @stmt = N'INSERT Users (Name) VALUES (@name)', @params = N'@name nvarchar(100)', @name = <user input>
This time, if the user were to send the same input, rather than executing the query that the user embedded in the string, the SQL Server will insert a row into the Users table, with the Name column set to ('Bob'); DROP TABLE Users; --'. This would obviously look a bit strange, but it wouldn't break the application or breach security.
- 樂高機器人EV3設(shè)計指南:創(chuàng)造者的搭建邏輯
- 西門子PLC與InTouch綜合應(yīng)用
- 來吧!帶你玩轉(zhuǎn)Excel VBA
- WordPress Theme Development Beginner's Guide(Third Edition)
- Windows環(huán)境下32位匯編語言程序設(shè)計
- 基于單片機的嵌入式工程開發(fā)詳解
- Machine Learning with Apache Spark Quick Start Guide
- OpenStack Cloud Computing Cookbook
- 統(tǒng)計挖掘與機器學習:大數(shù)據(jù)預(yù)測建模和分析技術(shù)(原書第3版)
- LMMS:A Complete Guide to Dance Music Production Beginner's Guide
- 網(wǎng)絡(luò)存儲·數(shù)據(jù)備份與還原
- Spark大數(shù)據(jù)商業(yè)實戰(zhàn)三部曲:內(nèi)核解密|商業(yè)案例|性能調(diào)優(yōu)
- 工業(yè)機器人入門實用教程
- 傳感器原理與工程應(yīng)用
- 樂高創(chuàng)意機器人教程(中級 上冊 10~16歲) (青少年iCAN+創(chuàng)新創(chuàng)意實踐指導(dǎo)叢書)