SqlDataReader Learning Notes and common application problems

Source: Internet
Author: User

SqlDataReader converted to string with space problem Believe that a lot of friends in the Database tutorial field and TextBox.Text attribute value comparison, the read out of the SqlDataReader object to string, found that even if the same value does not produce an equal result, the breakpoint method to view the results, the original SqlDataReader converted to string , the field type size is padded with a space, and then the space can be compared by using the trim () function of the string. This is because the database uses the Nvhar type, when write value than the specified value hours will be filled with space, so you should pay attention to remove the space problem.


Provides a way to read only incoming streams of rows from a SQL Server database. This class cannot be inherited.

Namespaces: 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

Using the SqlDataReader problem

The first question, DataReader's Read () method is sequential read, which can refer to the pointer, the first pointer to the head of the DataReader, but there is no data, reader data from the next address, if you want to get data need to use Read () to fetch.
The second question, HasRows, is whether to return the DataReader containing one or more results, and if so, tell the program that the department already exists.
The understanding here is that the data in DataReader no matter how many, he exists, just like you stand in front of a team, see is always the first person, if he does not walk away, you can not see the next person, but the number of the team will not because you do not see the change.

Application examples

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.executereader (commandbehavior.closeconnection);
return reader;

}
Catch
{
Conn.close ();
}
}

}


SqlDataReader can think of a forward only curosor. It's not like a dataset is a disconnected data link. It has been occupying connection [that is, always saving links]

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.