VS. NET (C #) Database Interface: SqlCommand object asynchronous execution case code, vs.net
When executing the Command object Command, you must wait until the Command is complete before other operations can be executed.
For example, if you execute the ExcuteNonQuery () method, the application will remain blocked until the data operation is completed successfully, or the connection is terminated abnormally and times out.
The idea of asynchronous execution is that when executing a command operation, you do not need to wait for the command operation to complete, and can process other operations concurrently.
BeginExecuteNonQuery and EndExcuteNonQuery are typical asynchronous operation services.
The BeginExecuteNonQuery method returns the System. IAsyncResult interface object. We can use the IsCompleted attribute of IAsyncResult to check whether the command is executed successfully.
Case code
Using System; using System. collections. generic; using System. linq; using System. text; using System. data; using System. data. sqlClient; namespace sqlCommand {class Program {static void Main (string [] args) {sqlCommandExe ();} private static SqlConnectionStringBuilder getConnDbStr () {// 1. construct the connection string SqlConnectionStringBuilder connStr = new SqlConnectionStringBuilder (); connStr. dataSource = "192.168.1.20"; connStr. initialCatalog = "mshDB_Debug"; connStr. userID = "developer"; connStr. password = "developer"; connStr. pooling = true; connStr. maxPoolSize = 40000; connStr. minPoolSize = 1; connStr. asynchronousProcessing = true; // display description return connStr;} private static StringBuilder getExeSqlStr () {// 2. construct the query statement string StringBuilder m_strSQL = new StringBuilder (); // for example, insert 0.5 million test users for (int I = 1; I <= 500000; ++ I) {m_strSQL.Append ("insert into tb_user (u_name, u_pwd, u_sex, u_age, u_class, u_address, u_phone)"); m_strSQL.Append ("values ('"); string name = "test customer" + I. toString (); m_strSQL.Append (name); m_strSQL.Append ("', '000000', 'female', 20, '000000', 'experimental Primary School of Pinghu city, Zhejiang Province ', '000000 '); ");} return m_strSQL;} private static void sqlCommandExe () {// 3. execute an SQL statement and follow these steps: // 3.1 generate a database connection object SqlConnectionStringBuilder strConn = getConnDbStr (); // obtain the connection string SqlConnection conn = new SqlConnection (strConn. connectionString); // 3.2 generate the SqlCommand object StringBuilder strSQL = getExeSqlStr (); SqlCommand cmd = new SqlCommand (strSQL. toString (), conn); cmd. commandType = CommandType. text; // 3.3 start asynchronous execution try {double time = 0; conn. open (); // Open the database connection Console. writeLine ("Data is inserted beigin... \ nTotal coast {0} seconds ", time * 0.001); IAsyncResult pending = cmd. beginExecuteNonQuery (); // start to execute asynchronous operations // check the asynchronous processing status while (pending. isCompleted = false) {System. threading. thread. sleep (1); time ++; Console. writeLine ("{0} s", time * 0.001);} if (pending. isCompleted = true) {Console. writeLine ("Data is inserted completely... \ nTotal coast {0} s ", time * 0.001);} cmd. endExecuteNonQuery (pending); // end Asynchronous Operation} catch (Exception e) {// Console. writeLine (e. message);} finally {conn. close (); conn. dispose ();} Console. read ();}}}
Enable SQL Server Resource Controller