Create an ASP. NET data storage layer (3)

Source: Internet
Author: User

Use Visual Studio. NET 2003 to write stored procedures
A data table defines how to store data in a database, but does not describe how to access data. We also need to know the Read and Write records so that the detailed information of the selected rows and columns can be called again from the table. Developers usually write special query statements in their code to read and write data. This will not only lead to low efficiency, but also bring security issues. In this application, all data access work will be handled through the SQL Server stored Procedure (stored procedures, sometimes referred to as "stored procs" or "sprocs. Using Stored Procedures improves the performance of the solution and makes it safer. In addition, using stored procedures can increase the abstraction level of the data layer, thus protecting other parts of the solution from the impact of small data la S and format changes. This makes your solution more reliable and easy to maintain.
Why not use special query statements?
We often see the following articles and sample code:
Private Function GetSomeData (ByVal ID As Integer) As SqlDataReader
Dim strSQL As String
StrSQL = "SELECT * FROM MyTable where id =" & ID. ToString ()
Cd = New SqlCommand
With cd
. CommandText = strSQL
. CommandType = CommandType. Text
. Connection = cn
. Connection. Open ()
Return. ExecuteReader (CommandBehavior. CloseConnection)
End
End Function
The above Code does not meet the requirements for the following reasons. First, if you nest SQL query statements in the code, you must edit and re-compile the code layer as long as the data layer changes. This will cause a lot of inconvenience. It may also cause other errors, and usually lead to confusion between data services and code.
Second, if you use a string that is not verified by the input ("... where id = "& ID. toString (), which may expose your application to hacker attacks. More importantly, this gives malicious users the opportunity to add other SQL keywords to your code. For example, based on your input mode, a malicious user can enter not only 13 or 21 as valid table IDs, but also 13; delete from users or other statements that may cause harm. Perfect input verification can protect your system from most SQL insert code attacks. Therefore, it is best to delete all built-in SQL statements completely, making it difficult for attackers to abuse your application data.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.