asp.net sqlhelper data Access Layer Use _ Practical skills

Source: Internet
Author: User
This article mainly introduces the use of SqlHelper.
Every project needs to use the data access layer, I do is no exception, but I make the data access layer into a stand-alone project, there is no great purpose, the data access layer, just do data access, does not contain any logic.

Why use the data access layer?
If you do not use the data access layer, then your code will appear a lot of SqlConnection, SqlCommand, SqlDataReader, Open, close ... These classes and methods, and the amount of code is very large, let you annoying, and the code written, in fact, are physically active, no technical content. So we need to encapsulate the data access layer for easy reuse. Microsoft has done the work for us, that is sqlhelper, the following diagram is the SqlHelper class diagram:

Here's how I use this.

Executes a stored procedure that has no return value:
create proc P_delete@id int
As
Delete table1 where id= @id

This is a stored procedure that deletes data, and only one sentence is invoked: Sqlhelper.executenonquery (Sqlhelper.conn, "P_delete", id); The ID here is type int, Rather than the SqlParameter type. The call is the public static int ExecuteNonQuery (string connectionString, string spname, params object[] parametervalues) method that is invoked. Analogy Returns a DataSet type: SqlHelper.ExecuteDataset (Sqlhelper.conn, Pro, parameter list); parameters are also passed directly without constructing SqlParameter ... Well, this article is mainly to introduce a small skill, is params object[] use method, let the master laughed at
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.