Review: Ado. Net Quick Start (1)
The usage of connection, datareader, Comand, parameters, and stored procedures is briefly mentioned. Now we will learn more deeply.
1. Usage of datareader:
DatareaderRetrieve read-only and inbound data streams from the database. The query result is returned when the query is executed and stored in the network buffer of the client until you useDatareaderOfReadMethod to send requests to them. UseDatareaderCan improve the applicationProgramThe reason is that it immediately retrieves data as long as the data is available, and (by default) onceStore only one row in the memory,Reduces system overhead.
For the example, see the previous article, which describes how to use datareader. In a project, sometimes a field in an object class is another entity Ray, and there is a foreign key relationship. The following object class source code has two such relationships (highlightedCode):
Code
Using System;
Using System. Collections. Generic;
Using System. text;
Namespace Bookshop. Model
{
[Serializable]
Public Class Book
{
/// <Summary>
/// Book No.
/// </Summary>
Private Int ID;
Public int id
{< br> Get { return ID ;}
set {id = value ;}
}
//
// book title
//
private string title;
Public string title
{< br> Get { return title ;}
set {Title = value ;}
}
//
// author
///
private string author;
Public string author
{< br> Get { return author ;}
set {author = value ;}
}
//
// Book Publishing House
//
private publisher publisher;
Public publisher publisher
{< br> Get { return publisher ;}
set {publisher = value ;}
}
//
// book publication date
//
private datetime publishdate;
Public datetime publishdate
{< br> Get { return publishdate ;}
set {publishdate = value ;}
}
//
// book ISBN No.
//
private string ISBN;
Public string ISBN
{< br> Get { return ISBN ;}
set {ISBN = value ;}
}
//
// total number of words in a book
//
private int wordscount;
Public int wordscount
{< br> Get { return wordscount ;}
set {wordscount = value ;}
}
//
// book price
//
private decimal unitprice;
Public decimal unitprice
{< br> Get { return unitprice ;}
set {unitprice = value ;}
}
//
// Book Description
//
private string contentdescription;
Public string contentdescription
{< br> Get { return contentdescription ;}
set {contentdescription = value ;}
}
//
// author description
//
private string authordescription;
Public string authordescription
{< br> Get { return authordescription ;}
set {authordescription = value ;}
}
//
// author comments
//
private string editorcomment;
Public string editorcomment
{< br> Get { return editorcomment ;}
set {editorcomment = value ;}
}
//
// book catalog
//
private string TOC;
Public string TOC
{< br> Get { return TOC ;}
set {TOC = value ;}
}
//
// classification of books
//
private Category category;
Public Category Category
{< br> Get { return category ;}
set {Category = value ;}
}
//
// click
//
private int clicks;
Public IntClicks
{
Get{ReturnClicks ;}
Set{Clicks=Value ;}
}
}
}
In this case, an exception may occur when you use datareader, because when the Code reads the foreign key, the foreign key also needs to use the connection, and an exception will be thrown.
Dynamic Interaction with data, such as binding to a Windows form control or combining and associating data from multiple sources.
A large amount of data is processed without the need to maintain an open connection with the data source, so that the connection is released to other clients. It is more appropriate to use dataset or able. '
You may not understand it, but you can simply remember that when the entity class or database design has a primary-foreign key relationship, you should be cautious when using datareader! But it doesn't matter. Many experiences have been learned from debug.
Just like Microsoft's video, debugging for love.