C # automatic setting and assignment of SQL Parameters

Source: Internet
Author: User

Introduction: on a certain day of a year, the company's Web programs were injected, resulting in a database mess. CIOs are furious and require that all database interactions be performed in the future only using SP. Therefore, even a query statement without any parameters must go to the DB to add an SP...

Project Example: starting a new project, the business process is complicated, and there are a lot of database interactions. SP writing is annoying, especially setting Command Parameters in the DAL, complicated and cost-effective, it is easy to make mistakes when writing too much, because we should not only consider the number of parameters, but also consider the passed value pair and Value Type pair, it is especially annoying for actions such as Update. Writing a Table with dozens of fields one by one is not a simple task. The typical code is as follows:

Typical setting of SQL Parameters
 Private readonly string _ mConnectionString = ConstParameter. ConnectionString; // obtain the ConnectionString, which is used to obtain the String of the corresponding development environment based on Web. Config.
Var conn = new SqlConnection (_ mConnectionString); // a New Connection
Cmd = conn. CreateCommand (); // a New Command
Cmd. CommandType = CommandType. StoredProcedure; // you can specify the Command type.
Cmd. CommandText = "Store Procedure Name"; // set SP Name // The following is the main code segment to be resolved in this article .....
Cmd. Parameters. Add (new SqlParameter ("@ Para1", SqlDbType "));
Cmd. Parameters ["@ Para1"]. Value = Para1Value;
Cmd. Parameters. Add (new SqlParameter ("@ Para2", SqlDbType "));
Cmd. Parameters ["@ Para2"]. Value = Para2Value;
Cmd. Parameters. Add (new SqlParameter ("@ Para3", SqlDbType "));
Cmd. Parameters ["@ Para3"]. Value = Para3Value;
.....

 

Process: it is very painful to write a piece of SP. After the debugging passes, I enter the UAT stage, and I am also relatively easy. Go to the blog Park and see a blog post Attribute in. NET programming applications (III), I found that the situation is basically the same as what I encountered. After reading it completely, I feel that using Attribute is really different. To see the implementation method, I mainly used reflection, so, why don't I use reflection to write a simple class? Shouldn't the implementation be complex? Two hours later, I handed in the first version, so I am not talking about it. Read the code first:

 

First define an object class
     public class ModelTestEvents
{
public int TestEventID { get; set; }
public string EpisodeStatus { get; set; }
public string SerialNumber { get;
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.