Batch insert and update

Source: Internet
Author: User
I recently learned what a three-tier architecture is, that is, this figure, Http://msdn.microsoft.com/library/en-us/dnbda/html/f00aa01.gif
Http://msdn.microsoft.com/library/en-us/dnbda/html/distapp.asp
Model layer, with anemia entity,
[Serializable]
Public class customerinfo
{
# Region private variables
Private system. guid _ customerid;
Private system. String _ name;
# Endregion

# Region Constructors
# Endregion

# Region Properties
Public System. guid customerid
{
Get {return _ customerid ;}
Set {_ customerid = value ;}
}

Public System. string name
{
Get {return _ name ;}
Set {_ name = value ;}
}
# Endregion
}
The data access layer uses El:
Public class customerdataservices: idataservices
{
# Region private field

# Endregion
Public customerinfocollection selectcustomersbynameinexact (system. string name)
{
Database DB = databasefactory. createdatabase ();
Dbcommand cmd = dB. getstoredproccommand (Sr. selectcustomersbynameinexact );
DB. addinparameter (CMD, "name", dbtype. String, name );

List <customerinfo> customers = new list <customerinfo> ();
Try
{
Using (idatareader DR = dB. executereader (CMD ))
{
While (dr. Read ())
{
Customerinfo customer = new customerinfo ();
Customer. customerid = (system. guid) Dr ["customerid"];
Customer. Name = (system. String) Dr ["name"];

MERs. add (customer);
}< br> dr. close ();
}< BR >}< br> catch (invalidoperationexception ex) // catch (dataexception ex)
....
return customers. count> 0? MERs: NULL;
}

Public bool insertcustomer (system. guid customerid, system. string name)
{
Database DB = databasefactory. createdatabase ();
Dbcommand cmd = dB. getstoredproccommand (Sr. insertcustomer );

Cmd = dB. getstoredproccommandwrapper (Sr. insertcustomer );
DB. addinparameter (CMD, "customerid", dbtype. guid, customerid );
DB. addinparameter (CMD, "name", dbtype. String, name );

bool ret = false;
try
{< br> dB. executenonquery (CMD);
ret = true;
}< br> catch (dataexception ex)
......
return ret;
}< BR >}

I suddenly found that when I add an order, I need to add multiple order items. How can I add and update Multiple order items in batches?
my original data operations are stored procedures.
Method 1: Call the method of inserting a record cyclically. I added 30-records at a time, and it was not very good.
Method 2: Create constant SQL text files for different databases, put the SQL text in the Code file just like pet shop, and create a static readonly constant file ). Load SQL Text Using Reflection Based on configuration. When batch addition and updating are performed, concatenate SQL text in cyclic statements and add command parameters, just like pet shop. This method is troublesome.
method 3: Define a class similar to dbcommandbuilder for different databases to obtain commands for batch addition and update. The specific implementation method of this method is not carefully considered.
Method 4: Send fields of objects updated and added in batches to the stored procedure with a repeating string as parameters, parse the string during the stored procedure, and splice the SQL text. This method is obviously risky for SQL injection.
what better method should I use? Let's talk about how to deal with similar problems. Thank you.

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.