This is the article backup, source (My Site) [. NET 4.5] ado.net/asp.net asynchronous access to the database using async and Await
Http://www.dotblogs.com.tw/mis2000lab/archive/2014/05/08/ado.net4.5_async_await_20140508.aspx
The previous ADO can also be "asynchronous" (Async, Continental: Async), you can refer to the Kkbruce 2009/11-month article:
SQLCOMMAND the asynchronous Stroke
Http://blog.kkbruce.net/2009/11/sqlcommand.html
However, the following new version of. NET 4.5 & VS 2012 (included) has new approaches
The following is a description of the MSDN Web site (excerpt):
Visual Studio introduces a simplified approach (asynchronous programming) that leverages the. NET Framework 4.5 and asynchronous support in Windows runtime. The compiler will do the hard work that the developer used to do in the past, and your application still retains a logical structure similar to the synchronization code. As a result, you can easily get all the benefits of asynchronous programming.
Asynchrony is important for activities that may be blocked, such as when the application accesses the Web. Access to network resources can sometimes become slow or delayed. if such activities are blocked during the synchronization process, the entire application must wait. in an asynchronous process, the application can proceed with other work that does not depend on the WEB resource until the possible blocking work is complete.
Visual Basic in the Async and the Await keywords, and C # in the Async and the await keywords, are the core of asynchronous programming. using these two keywords, you can use resources to establish asynchronous methods in the. NET Framework or Windows runtime, almost as easily as you would build a synchronization method. Asynchronous methods defined through async and await are called async methods.
......................................................................................................................... ...............................................
The following uses ASP. NET as an example, using ADO DataReader to write
Examples from the four models provided in "my books"
First, declare the System.Threading.Tasks namespace
C #----using System.Threading.Tasks;
VB----Imports System.Threading.Tasks
If necessary, use NuGet on your project or website
Search for "Microsoft.bcl.Async" and install
The next C # post-program code, the program is as follows
Please add the Async keyword in front of the event!! ***
Protected Async void Button1_Click (object sender, EventArgs e)
{
await Mis2000lab_async ();
}
"Async" function written by yourself * * *
Please add the Async keyword, in front of the function!! ***
VB notation-- Protected Shared Async Function mis2000lab_async () as Task (of System.Threading.Tasks.Task)
protected static async Task mis2000lab_async ()
{
SqlConnection Conn = new SqlConnection ("Your own DB link string");
SqlDataReader dr = null;
SqlCommand cmd = new SqlCommand ("Select author from Test", Conn);
Try
{
= = First, link database. Asynchronous usage only in. NET 4.5 (including) subsequent new versions
Old notation Conn.Open (); ----Nexus DB
await Conn.OpenAsync();
= = Second, execute the SQL instruction.
The old syntax is Dr = cmd. ExecuteReader (); ----Execute the SQL instruction, take out the data
Dr = await cmd. ExecuteReaderAsync(commandbehavior.sequentialaccess);
= = Third, the free play, the implementation of the results presented to the screen.
= = Write your own loop = =
The old notation while (Dr. Read ())
while (await Dr. ReadAsync())
{
if (await Dr. IsDBNullAsync(1))//1 means True,dbnull
{
HttpContext.Current.Response.Write ("* * * * null***");
}
Else
{
HttpContext.Current.Response.Write (dr["author"] + "<br/>");
}
}
}
//...... (not finished) ... The following "Close resource" program code has not changed, please refer to the book
About ADO. NET program examples can be found in my previous article:
[ADO] Standard example for ASP. DataReader (Code behind version)
Http://www.dotblogs.com.tw/mis2000lab/archive/2008/04/24/3446.aspx
Or my recording of a teaching film:https://www.youtube.com/watch?v=zeXgLVqSy50
......................................................................................................................... ...............................................
We can find the difference with the previous
1. The program code is more simple. Take the old program to modify, change the amplitude is very small.
2. Previously used begin ... Method with End ... The methods are gone and not written to IAsyncResult.
The code inside the program is useful-- commandbehavior.sequentialaccess
Provides methods for DataReader to use large binary values to process data columns (records) that contain data rows (fields). SequentialAccess does not load the entire data column, but instead enables DataReader to load the data as a data stream. You can then use the. GetBytes () or. The GetChars () method specifies the byte position to start reading the job and the limit buffer size of the data being passed back.
When you specify SequentialAccess, you must read them in the order in which the data rows (fields) are returned, but you do not need to read each data row (field). Once you have read the past data location in the data stream that was passed in, the data at that location and before that location can no longer be read from DataReader. When using OleDbDataReader, you can read the current data row (field) value repeatedly until the read crosses it. When you use SqlDataReader, you can read only the values of the data rows (fields).
my annotations: The emphasis on the above, illustrates the datareader"forward (Forward), read-only feature. You can refer to my recorded instructional video- Https://www.youtube.com/watch?v=oY7jd0ABXeM
For related articles, see:
[ADO] DataReader Standard example of For ASP. Behind version #2-- CommandBehavior
Http://www.dotblogs.com.tw/mis2000lab/archive/2010/12/01/datareader_2_commandbehavior.aspx
[MSDN] CommandBehavior Enumeration Types
Http://msdn.microsoft.com/zh-tw/library/system.data.commandbehavior.aspx
......................................................................................................................... ...............................................
Examples of this article are the most and most clearly:[MSDN] Asynchronous Programming
http://msdn.microsoft.com/zh-tw/library/hh211418 (v=vs.110). aspx
Microsoft MSDN has a lot of commentary & program code examples
Use Async and the Await Designing Asynchronous Programs (C # and Visual Basic)
Http://msdn.microsoft.com/zh-tw/library/hh191443.aspx
Walkthrough : Using Async and the Await Access Web (C # and Visual Basic)
Http://msdn.microsoft.com/zh-tw/library/hh300224.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-1
Using SqlDataReader ' s new async methods in. Net 4.5, part 2:examples
http://blogs.msdn.com/b/adonet/archive/2012/07/15/ Using-sqldatareader-s-new-async-methods-in-net-4-5-beta-part-2-examples.aspx
I also refer to this book (Simplified Chinese version), the book is very thin, but the writing is very good.