SqlDataReader Study Notes and common application problems

Source: Internet
Author: User

Sqldatareader is converted to string with spaces. I believe many of my friends are doing database tutorial fields and textbox. when comparing the values in the text attribute, convert the read sqldatareader object to a string. Even if the same value does not produce equal results, you can use the breakpoint method to view the results, after the original sqldatareader is converted to a string, space is used to fill in the field type size. Finally, we can use the string trim () function to remove spaces for comparison. This is because the database uses the nvhar type. When the written value is smaller than the specified value, spaces are filled. Therefore, you should pay attention to the space removal problem.


Provides a method to read rows from the SQL server database only into the stream. This class cannot be inherited.

Namespace: system. data. sqlclient
Assembly: system. data (in system. data. dll)

Statement

Public class sqldatareader
Inherits dbdatareader
Implements idatareader, idisposable, idatarecord


'Usage

Dim instance as sqldatareader

J #
Copy

Public class sqldatareader extends dbdatareader implements idatareader, idisposable,
Idatarecord

Sqldatareader

The first problem is that the read () method of datareader is sequential reading. You can refer to the pointer. At first, the Pointer Points to the header of datareader, but there is no data, the data in reader starts from the next address. If you want to obtain the data, read () is used again and again.
The second problem is that hasrows returns whether the datareader contains one or more results. If so, you can directly tell the program that this department already exists.
It should be understood that no matter how many pieces of data in datareader exist, it is like standing in front of a team and seeing the first person. If he doesn't walk away, you cannot see the next person, but the number of people in the team will not change because you cannot see it.

Application Instance

Public static sqldatareader executereader (string str)
{
Using (sqlconnection conn = new sqlconnection ())
{
Try
{
Conn = createconnection ();
If (conn. state = connectionstate. closed) conn. open ();
Sqlcommand cmd = new sqlcommand (str, conn );

Sqldatareader reader = cmd.exe cutereader (commandbehavior. closeconnection );
Return reader;

}
Catch
{
Conn. close ();
}
}

}


Sqldatareader can be considered as a forward only curosor. Unlike dataset, It is a broken data link. It keeps occupying connection [that is, saving the LINK]

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.