Use sqlhelper to perform the add operation

Source: Internet
Author: User

I haven't touched. net in a year, and almost all of them are forgotten. I have been familiar with this in the past few days. First, I am familiar with using the sqlhelper and property attribute classes to add them.

1. Define attributes

 

Using system; <br/> namespace shop. model <br/> {<br/> [serializable] <br/> // <summary> <br/> // order information attribute order entity <br/>/ // </Summary> <br/> public class order <br/> {<br/> private int orderid; <br/> private string name; <br/> private int number; <br/> private datetime date; <br/> Public Order () <br/> {<br/> // todo: add the constructor logic here <br/> // <br/>}< br/> // <summary> <br/> // custom constructor <br />/// </Summary> <br/> /// <Param name = "orderid"> order id </param> <br/> /// <Param name = "name"> order name </param> <br/> // <Param name = "Number"> order quantity </param> <br/> // <param name = "datetime"> order date </param> <br/> Public Order (INT orderid, string name, int number, datetime date) <br/>{< br/> This. orderid = orderid; <br/> This. name = Name; <br/> This. number = number; <br/> This. date = date; <br/>}< br/> // properties <br/> Public int orderid <br/>{< br/> get {return orderid ;} <br/> set {orderid = value ;}< br/>}< br/> Public string name <br/>{< br/> get {return name ;} <br/> set {name = value ;}< br/>}< br/> Public int number <br/>{< br/> get {return number ;} <br/> set {number = value ;}< br/>}< br/> Public datetime date <br/>{< br/> get {return date ;} <br/> set {date = value ;}< br/>}< br/>}

2. Define a data access layer interface. The Interface Name Is iorder. The Code is as follows:

Using system; <br/> using shop. model; <br/> namespace shop. idal <br/> {<br/> /// <summary> <br/> // Summary of ioder <br/> /// </Summary> <br /> Public interface iorder <br/>{< br/> // Add an order <br/> void insertorder (Order orderinfo ); <br/> // <summary> <br/> // obtain order information by ID <br/> /// </Summary> <br/> /// <Param name = "orderid"> </param> <br/> // <returns> </returns> <br/> order getorder (INT orderid ); </P> <p >}< br/>

3. Implement the iorder Interface

Using system; <br/> using system. data; <br/> using system. data. sqlclient; <br/> using system. configuration; <br/> using system. text; <br/> using system. collections. generic; <br/> using shop. dbutility; <br/> using shop. model; <br/> using shop. idal; <br/> namespace shop. sqlserverdal <br/> {<br/> /// <summary> <br/> // abstract of order <br/> /// </Summary> <br /> public class order: iorder <br/>{< br/> private const string SQL _insert_order = "declare @ ID int; declare @ err int; insert into orderinfo values (@ orderid, @ name, @ number, @ datetime); select @ ID = @ identity; select @ err = @ error; "; <br/> private const string SQL _select_order =" select * From orderinfo "; // SQL statement <br/> Public Order () <br/> {<br/> // todo: add the constructor logic here <br/> // </P> <p >}< br/> // perform the add operation <br/> Public void insertorder (Order orderinfo) <br/>{< br/> stringbuilder strsql = new stringbuilder (); <br/> sqlparameter [] orderparams = getorderparameter (); // Basic Order Parameters <br/> sqlcommand cmd = new sqlcommand (); <br/> orderparams [0]. value = orderinfo. orderid; <br/> orderparams [1]. value = orderinfo. name; <br/> orderparams [2]. value = orderinfo. number; <br/> orderparams [3]. value = orderinfo. date; <br/> sqlhelper. executenonquery (sqlhelper. connectionstringorderdistributedtransaction, commandtype. text, SQL _insert_order, orderparams ); // use the static method defined in sqlhelper to add data. </P> <p >}< br/> // <summary> <br/> /// obtain the Order Parameters <br/> /// </Summary> <br/> // <returns> </returns> <br/> Public static sqlparameter [] getorderparameter () <br/> {<br/> sqlparameter [] parames = sqlhelperparametercache. getcachedparameterset (sqlhelper. connectionstringorderdistributedtransaction, SQL _insert_order); // checks whether cache parameters are cached and obtain cached parameters. <br/> If (parames = NULL) <br/>{< br/> parames = new sqlparameter [] {<br/> New sqlparameter ("@ orderid", sqldbtype. int, 10), <br/> New sqlparameter ("@ name", sqldbtype. varchar, 10), <br/> New sqlparameter ("@ number", sqldbtype. int, 10), <br/> New sqlparameter ("@ datetime", sqldbtype. datetime), </P> <p >}; <br/> // parameter settings <br/> sqlhelperparametercache. cacheparameterset (sqlhelper. connectionstringorderdistributedtransaction, SQL _insert_order, parames); <br/>}// sets the cache parameter <br/> return parames; <br/>}< br/> // obtain order data <br/> Public Order getorder (INT orderid) <br/>{< br/> order orderinfo = New Order (); <br/> sqlparameter pare = new sqlparameter ("@ orderid", sqldbtype. INT); <br/> pare. value = orderid; <br/> using (sqldatareader SD = sqlhelper. executereader (sqlhelper. connectionstringorderdistributedtransaction, commandtype. text, SQL _select_order, PARE) {<br/> If (SD. read () <br/>{< br/> orderinfo = New Order (orderid, SD. getstring (1), SD. getint32 (2), SD. getdatetime (3); <br/>}</P> <p >}< br/> return orderinfo; </P> <p >}< br/>

4. perform operations on the Web Client (default. aspx. CS)

A button event is defined on this page and added to its implementation. The Code is as follows:

Public partial class _ default: system. web. UI. page <br/>{< br/> protected void page_load (Object sender, eventargs E) <br/>{</P> <p >}< br/> protected void button#click (Object sender, eventargs E) <br/>{< br/> order orderinfo = New Order (2, "Lee", 20, datetime. now); // assign values to attributes <br/> shop. sqlserverdal. order o = new shop. sqlserverdal. order (); <br/> O. insertorder (orderinfo); // call the add method. In this way, the data is successfully added </P> <p >}< br/>}

5. Use sqlhelper. CS to add the database. In this way, the parameter issue is well handled. Improves security and performance.

I am afraid that I will forget it again. I will make a memorandum.

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.