MM test the database affordability-(for example, sqlbulkcopy)

Source: Internet
Author: User

// --------- Note that the same data can still be added -- up to 4031040 data records can be inserted in batch for testing.
Environment
Sql2005 + net2005-system Vista-2 GB memory, cpu2.6 -- in the end, all data is cleared, but the blog file that covers the data is about GB.

// --------- Note that the same data can still be added -- up to 4031040 data records can be inserted in batch for testing.

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Using system. Data. sqlclient;
Public partial class default4: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
String blogstring;
String dbtextstirng;

// Retrieval source
Sqlconnection blogconnection = new sqlconnection ();
// Insert Source
Sqlconnection dbtextconnection = new sqlconnection ();

Sqlcommand blogcommand = new sqlcommand ();

Sqldatareader blogreader;

Blogstring = system. configuration. configurationmanager. connectionstrings ["connectionstring"]. connectionstring;
Dbtextstirng = system. configuration. configurationmanager. connectionstrings ["connectionstringinfo"]. connectionstring;


Blogconnection. connectionstring = blogstring;
Blogcommand. Connection = blogconnection;
// ---- Set the data willingness to be inserted into other tables
Blogcommand. commandtext = "select * From mm ";
Blogcommand. commandtype = commandtype. text;

Blogcommand. Connection. open ();

// -- Enables you to effectively load SQL Server tables in batches with data from other sources.
// Connectionstring defines the connection string to be opened and used by the sqlbulkcopy instance. The combination of values in the copyoptions sqlbulkcopyoptions enumeration. This enumeration determines which data source rows are copied to the target table.
// ---------------------------------------------- Restore the data to the connection string
Sqlbulkcopy blogsqlbulkcopy = new sqlbulkcopy (dbtextstirng, sqlbulkcopyoptions. useinternaltransaction );
// Sqlbulkcopyoptions enumeration member Name Description
// Checkconstraints check constraints while inserting data. By default, constraints are not checked.
// Use the default value for all options.
// When specified by firetriggers, the server triggers the insert trigger for the row inserted into the database.
// Keepidentity retains the source ID value. If this parameter is not specified, the target assigns an id value.
// Keepnulls retains the null value in the target table regardless of the default value. If not specified, the null value is replaced by the default value (if applicable ).
// Tablelock obtains the batch update lock during the batch copy operation. If not specified, the row lock is used.
// If useinternaltransaction is specified, each batch of batch replication operations will occur in the transaction. Argumentexception occurs if this option is specified and the sqltransaction object is provided for the constructor.

// Set the target table to copy
Blogsqlbulkcopy. destinationtablename = "GG ";

// -- Create a reference for the source column in the target column ---------- skip the ing if the source column is the same as the target Column
Blogsqlbulkcopy. columnmappings. Add ("ID", "ID ");
Blogsqlbulkcopy. columnmappings. Add ("mmname", "mmname ");

// -- Another method for adding and drinking
Sqlbulkcopycolumnmapping urladdress = new sqlbulkcopycolumnmapping ("urladdress", "urladdress ");
Blogsqlbulkcopy. columnmappings. Add (urladdress );

//-The number of seconds allowed to complete the operation before the timeout.
Blogsqlbulkcopy. bulkcopytimeout = 50000000;

// It occurs when the number of rows specified by the policyafter attribute is processed each time.
Blogsqlbulkcopy. sqlrowscopied + = new sqlrowscopiedeventhandler (this. onrowscopied );

// Define the number of rows to be processed before a notification event is generated.
Blogsqlbulkcopy. policyafter = 1;

Blogreader = blogcommand. executereader ();

Try
{
// ---------- Copy all rows from the data source to the target table specified by the destinationtablename attribute of the sqlbulkcopy object.
Blogsqlbulkcopy. writetoserver (blogreader );
}
Catch (syntaxerrorexception ex)
{
Response. Write (ex. Message );
}
Finally
{
Blogreader. Close ();
}


}

Private void onrowscopied (Object sender, sqlrowscopiedeventargs ARGs)
{
Response. Write (ARGs. rowscopied. tostring () + "<br> ");
}
}

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.