System.Data.SqlClient Namespace
Provides a means of reading a forward-only stream of rows from an SQL Server™ database.
Visibility |
Name |
Parameters |
Return Type |
public |
Close |
( )
|
Void
|
public |
GetBoolean |
(
Int32
i
)
|
Boolean
|
public |
GetByte |
(
Int32
i
)
|
Byte
|
public |
GetBytes |
(
Int32
i
,
Int64
dataIndex
,
Byte
buffer
,
Int32
bufferIndex
,
Int32
length
)
|
Int64
|
public |
GetChar |
(
Int32
i
)
|
Char
|
public |
GetChars |
(
Int32
i
,
Int64
dataIndex
,
Char
buffer
,
Int32
bufferIndex
,
Int32
length
)
|
Int64
|
public |
GetDataTypeName |
(
Int32
i
)
|
String
|
public |
GetDateTime |
(
Int32
i
)
|
DateTime
|
public |
GetDateTimeOffset |
(
Int32
i
)
|
DateTimeOffset
|
public |
GetDecimal |
(
Int32
i
)
|
Decimal
|
public |
GetDouble |
(
Int32
i
)
|
Double
|
public |
GetEnumerator |
( )
|
IEnumerator
|
public |
GetFieldType |
(
Int32
i
)
|
Type
|
public |
GetFieldValue |
(
Int32
i
)
|
T
|
public |
GetFieldValueAsync |
(
Int32
i
,
CancellationToken
cancellationToken
)
|
Task`1
|
public |
GetFloat |
(
Int32
i
)
|
Single
|
public |
GetGuid |
(
Int32
i
)
|
Guid
|
public |
GetInt16 |
(
Int32
i
)
|
Int16
|
public |
GetInt32 |
(
Int32
i
)
|
Int32
|
public |
GetInt64 |
(
Int32
i
)
|
Int64
|
public |
GetName |
(
Int32
i
)
|
String
|
public |
GetOrdinal |
(
String
name
)
|
Int32
|
public |
GetProviderSpecificFieldType |
(
Int32
i
)
|
Type
|
public |
GetProviderSpecificValue |
(
Int32
i
)
|
Object
|
public |
GetProviderSpecificValues |
(
Object
values
)
|
Int32
|
public |
GetSchemaTable |
( )
|
DataTable
|
public |
GetSqlBinary |
(
Int32
i
)
|
SqlBinary
|
public |
GetSqlBoolean |
(
Int32
i
)
|
SqlBoolean
|
public |
GetSqlByte |
(
Int32
i
)
|
SqlByte
|
public |
GetSqlBytes |
(
Int32
i
)
|
SqlBytes
|
public |
GetSqlChars |
(
Int32
i
)
|
SqlChars
|
public |
GetSqlDateTime |
(
Int32
i
)
|
SqlDateTime
|
public |
GetSqlDecimal |
(
Int32
i
)
|
SqlDecimal
|
public |
GetSqlDouble |
(
Int32
i
)
|
SqlDouble
|
public |
GetSqlGuid |
(
Int32
i
)
|
SqlGuid
|
public |
GetSqlInt16 |
(
Int32
i
)
|
SqlInt16
|
public |
GetSqlInt32 |
(
Int32
i
)
|
SqlInt32
|
public |
GetSqlInt64 |
(
Int32
i
)
|
SqlInt64
|
public |
GetSqlMoney |
(
Int32
i
)
|
SqlMoney
|
public |
GetSqlSingle |
(
Int32
i
)
|
SqlSingle
|
public |
GetSqlString |
(
Int32
i
)
|
SqlString
|
public |
GetSqlValue |
(
Int32
i
)
|
Object
|
public |
GetSqlValues |
(
Object
values
)
|
Int32
|
public |
GetSqlXml |
(
Int32
i
)
|
SqlXml
|
public |
GetStream |
(
Int32
i
)
|
Stream
|
public |
GetString |
(
Int32
i
)
|
String
|
public |
GetTextReader |
(
Int32
i
)
|
TextReader
|
public |
GetTimeSpan |
(
Int32
i
)
|
TimeSpan
|
public |
GetValue |
(
Int32
i
)
|
Object
|
public |
GetValues |
(
Object
values
)
|
Int32
|
public |
GetXmlReader |
(
Int32
i
)
|
XmlReader
|
public |
IsDBNull |
(
Int32
i
)
|
Boolean
|
public |
IsDBNullAsync |
(
Int32
i
,
CancellationToken
cancellationToken
)
|
Task`1
|
public |
NextResult |
( )
|
Boolean
|
public |
NextResultAsync |
(
CancellationToken
cancellationToken
)
|
Task`1
|
public |
Read |
( )
|
Boolean
|
public |
ReadAsync |
(
CancellationToken
cancellationToken
)
|
Task`1
|
|
To create an SqlDataReader, you must call the ExecuteReader method of the SqlCommand object, rather than directly using a constructor.
While the SqlDataReader is in use, the associated SqlConnection is busy serving the SqlDataReader, and no other operations can be performed on the SqlConnection other than closing it. This is the case until the Close method of the SqlDataReader is called.
Changes made to a resultset by another process or thread while data is being read may be visible to the user of the SqlDataReader. However, the precise behavior is timing dependent.
IsClosed and RecordsAffected are the only properties that you can call after the SqlDataReader is closed. Though the RecordsAffected property may be accessed at any time while the SqlDataReader exists, always call Close before returning the value of RecordsAffected to ensure an accurate return value.
NOTE: For optimal performance, SqlDataReader avoids creating unnecessary objects or making unnecessary copies of data. As a result, multiple calls to methods such as GetValue return a reference to the same object. Use caution if you are modifying the underlying value of the objects returned by methods such as GetValue.
The following example shows one way to implement an SqlDataReader in a Web Forms page. Here, the entire logic is encapsulated in a Page_Load event handler that essentially:
- establishes a connection to the database,
- initializes an instance of a DataReader to contain the database information,
- and then enumerates the contents of the Datareader into an HTML table.
protected void Page_Load ( Object Src, EventArgs e ) {
// specify the data source
SqlConnection myConn = new SqlConnection (
"server=localhost; trusted_connection=yes; database=northwind" );
// define the command query
String query = "SELECT ProductName, ProductDescription FROM Products";
SqlCommand myCommand = new SqlCommand ( query, myConn );
// open the connection and instantiate a datareader
myConn.Open ( );
SqlDataReader myReader = myCommand.ExecuteReader ( );
// display datareader contents into html table
// first open the table and set up the table headers
html += "<table cellspacing=1 class='data' width=90%>";
html += "<tr>";
html += "<th>Product Name</th>";
html += "<th>Description</th>";
html += "</tr>";
// loop thru the reader
while ( myReader.Read ( ) ) {
html += "<tr>";
html += "<td>" + myReader.GetString ( 0 ) + "</td>";
html += "<td>" + myReader.GetString ( 1 ) + "</td>";
html += "</tr>";
}
// close the table
html += "</table>";
// close the reader and the connection
myReader.Close ( );
myConn.Close ( );
}
</script>
protected sub Page_Load ( byVal Src as Object, byVal E as EventArgs )
' specify the data source
dim myConn as new SqlConnection ( & _
"server=localhost; trusted_connection=yes; database=northwind" )
' define the command query
dim query as String = "SELECT ProductName, ProductDescription FROM Products"
dim myCommand as new SqlCommand ( query, myConn )
' open the connection and instantiate a datareader
myConn.Open ( )
dim myReader as SqlDataReader = myCommand.ExecuteReader ( )
' display datareader contents into html table
' first open the table and set up the table headers
html &= "<table cellspacing=1 class='data' width=90%>"
html &= "<tr>"
html &= "<th>Product Name</th>"
html &= "<th>Description</th>"
html &= "</tr>"
' loop thru the reader
while myReader.Read ( )
html &= "<tr>"
html &= "<td>" + myReader.GetString ( 0 ) + "</td>"
html &= "<td>" + myReader.GetString ( 1 ) + "</td>"
html &= "</tr>"
end while
' close the table
html &= "</table>"
' close the reader and the connection
myReader.Close ( )
myConn.Close ( )
end sub
</script> |
|
C# |
VB |
Show me