Properties and Methods of the SqlDataReader Class
SqlDataReader.FieldCount
Syntax
Int32 FieldCount
Description
The Fieldcount property contains the number of fields in the current record.
Example
int FieldCount;
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select employee_id,
lastname, firstname from Employees", conn);
SqlDataReader reader = cmd.ExecuteReader();
FieldCount = reader.FieldCount;
SqlDataReader.IsClosed
Syntax
Boolean IsClosed
Description
The IsClosed property contains True if the SqlDataReader object is closed.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select employee_id,
lastname, firstname from Employees", conn);
SqlDataReader reader = cmd.ExecuteReader();
if(reader.IsClosed != true){
}
SqlDataReader.Item
Syntax
Object Item(String name)
Object Item(Int32 i)
Description
The Item property retrieves the value of a column in its native data format.
Example
string message = "";
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select employee_id,
lastname, firstname from Employees", conn);
SqlDataReader reader = cmd.ExecuteReader();
while(reader.Read()){
message = message + reader["employee_id"] + " " + reader["lastname"] +
", " + reader["firstname"] + "<br>";
}
reader.Close();
SqlDataReader.RecordsAffected
Syntax
Int32 RecordsAffected
Description
The RecordsAffected property contains the number of records affected by the query.
Example
int RecordsAffected;
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select employee_id,
lastname, firstname from Employees", conn);
SqlDataReader reader = cmd.ExecuteReader();
RecordsAffected = reader.RecordsAffected;
SqlDataReader.Close()
Syntax
Void Close()
Description
The Close() method closes the SqlDataReader object.
Example
SqlDataReader reader = cmd.ExecuteReader();
reader.Close();
SqlDataReader.GetBoolean()
Syntax
Boolean GetBoolean(Int32 i)
Description
The GetBoolean() method returns the value of a specified column as type Boolean.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname, from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
Boolean value;
while(reader.Read()){
value = reader.GetBoolean(0);
}
reader.Close();
SqlDataReader.GetByte
Syntax
Byte GetByte(Int32 i)
Description
The GetByte() method returns the value of a specified column as type Byte.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
Byte value;
while(reader.Read()){
value = reader.GetByte(0);
}
reader.Close();
SqlDataReader.GetBytes
Syntax
Int32 GetBytes(Int32 i, Int32 dataIndex, Byte[buffer],
Int32 bufferIndex, Int32 length)
Description
The GetBytes() method returns the value of a specified column as type Bytes.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
Byte[] value = new Byte[10];
while(reader.Read()){
reader.GetBytes(0, 0, value, 0, 10);
}
reader.Close();
SqlDataReader.GetChar()
Syntax
Char GetChar( Int32 i )
Description
The GetChar() method returns the value of a specified column as type Char.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
char value;
while(reader.Read()){
value = reader.GetChar(0);
}
reader.Close();
SqlDataReader.GetChars()
Syntax
Int32 GetChars(Int32 i, Int32 dataIndex, Char[] buffer,
Int32 bufferIndex, Int32 length)
Description
The GetChars() method returns the value of a specified column as type Char.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
char[] value = new char[10];
while(reader.Read()){
reader.GetChars(0, 0, value, 0, 10);
}
reader.Close();
SqlDataReader.GetDataTypeName()
Syntax
String GetDataTypeName(Int32 i)
Description
The GetDataTypeName() method returns a string that contains the data type of the specified field.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
string datatype = reader.GetDataTypeName(0);
reader.Close();
SqlDataReader.GetDateTime()
Syntax
DateTime GetDateTime(Int32 i)
Description
The GetDateTime() method returns the value of a specified column as type DateTime.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
DateTime value;
while(reader.Read()){
value = reader.GetDateTime(0);
}
reader.Close();
SqlDataReader.GetDecimal()
Syntax
Decimal GetDecimal(Int32 i)
Description
The GetDecimal() method returns the value of a specified column as type Decimal.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
decimal value;
while(reader.Read()){
value = reader.GetDecimal(0);
}
reader.Close();
SqlDataReader.GetDouble()
Syntax
Double GetDouble(Int32 i)
Description
The GetDouble() method returns the value of a specified column as type Double.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
double value;
while(reader.Read()){
value = reader.GetDouble(0);
}
reader.Close();
SqlDataReader.GetFieldType()
Syntax
Type GetFieldType(Int32 i)
Description
The GetFieldType() method returns the type of the specified field.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
Type dataType = reader.GetFieldType(0);
reader.Close();
SqlDataReader.GetFloat()
Syntax
Single GetFloat(Int32 i)
Description
The GetFloat() method returns the value of a specified column as type Float.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
float value;
while(reader.Read()){
value = reader.GetFloat(0);
}
reader.Close();
SqlDataReader.GetGuid()
Syntax
Guid GetGuid(Int32 i)
Description
The GetGuid() method returns the value of a specified column as type Guid.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
Guid value;
while(reader.Read()){
value = reader.GetGuid(0);
}
reader.Close();
SqlDataReader.GetInt16()
Syntax
Int16 GetInt16(Int32 i)
Description
The GetInt16() method returns the value of a specified column as type Int16.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
Int16 value;
while(reader.Read()){
value = reader.GetInt16(0);
}
reader.Close();
SqlDataReader.GetInt32()
Syntax
Int32 GetInt32(Int32 i)
Description
The GetInt32() method returns the value of a specified column as type Int32.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
Int32 value;
while(reader.Read()){
value = reader.GetInt32(0);
}
reader.Close();
SqlDataReader.GetInt64()
Syntax
Int64 GetInt64(Int32 i)
Description
The GetInt64() method returns the value of a specified column as type Int64.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
Int64 value;
while(reader.Read()){
value = reader.GetInt64(0);
}
reader.Close();
SqlDataReader.GetName()
Syntax
String GetName(Int32 i)
Description
The GetName() method returns a string that contains the name of the SQL field.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
string fieldname = reader.GetName(0);
reader.Close();
SqlDataReader.GetOrdinal()
Syntax
Int32 GetOrdinal(String name)
Description
The GetOrdinal() method returns an integer that contains the ordinal number of the specified field.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
int fieldOrdinal = reader.GetOrdinal(fieldname);
reader.Close();
SqlDataReader.GetSchemaTable()
Syntax
DataTable GetSchemaTable()
Description
The GetSchemaTable() method returns a data table that contains database schema for the SqlCommand object.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
DataTable aTable = reader.GetSchemaTable();
reader.Close();
SqlDataReader.GetSqlBinary()
Syntax
SqlBinary GetSqlBinary(Int32 i)
Description
The GetSqlBinary() method returns the value of a specified column as type SqlBinary.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlBinary value;
while(reader.Read()){
value = reader.GetSqlBinary(0);
}
reader.Close();
SqlDataReader.GetSqlBit()
Syntax
SqlBit GetSqlBit(Int32 i)
Description
The GetSqlBit() method returns the value of a specified column as type SqlBit.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlBit value;
while(reader.Read()){
value = reader.GetSqlBit(0);
}
reader.Close();
SqlDataReader.GetSqlByte()
Syntax
SqlByte GetSqlByte(Int32 i)
Description
The GetSqlByte() method returns the value of a specified column as type SqlByte.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlByte value;
while(reader.Read()){
value = reader.GetSqlByte(0);
}
reader.Close();
SqlDataReader.GetSqlDateTime()
Syntax
SqlDateTime GetSqlDateTime(Int32 i)
Description
The GetSqlDateTime() method returns the value of a specified column as type SqlDateTime.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlDateTime value;
while(reader.Read()){
value = reader.GetSqlDateTime(0);
}
reader.Close();
SqlDataReader.GetSqlDecimal()
Syntax
SqlDecimal GetSqlDecimal(Int32 i)
Description
The GetSqlDecimal() method returns the value of a specified column as type SqlDecimal.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlDecimal value;
while(reader.Read()){
value = reader.GetSqlDecimal(0);
}
reader.Close();
SqlDataReader.GetSqlDouble()
Syntax
SqlDouble GetSqlDouble(Int32 i)
Description
The GetSqlDouble() method returns the value of a specified column as type SqlDouble.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlDouble value;
while(reader.Read()){
value = reader.GetSqlDouble(0);
}
reader.Close();
SqlDataReader.GetSqlGuid()
Syntax
SqlGuid GetSqlGuid(Int32 i)
Description
The GetSqlGuid() method returns the value of a specified column as type SqlGuid.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlGuid value;
while(reader.Read()){
value = reader.GetSqlGuid(0);
}
reader.Close();
SqlDataReader.GetSqlInt16()
Syntax
SqlInt16 GetSqlInt16(Int32 i)
Description
The GetSqlInt16() method returns the value of a specified column as type SqlInt16.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlInt16 value;
while(reader.Read()){
value = reader.GetSqlInt16(0);
}
reader.Close();
SqlDataReader.GetSqlInt32()
Syntax
SqlInt32 GetSqlInt32(Int32 i)
Description
The GetSqlInt32() method returns the value of a specified column as type SqlInt32.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlInt32 value;
while(reader.Read()){
value = reader.GetSqlInt32(0);
}
reader.Close();
SqlDataReader.GetSqlInt64()
Syntax
SqlInt64 GetSqlInt64(Int32 i)
Description
The GetSqlInt64() method returns the value of a specified column as type SqlInt64.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlInt64 value;
while(reader.Read()){
value = reader.GetSqlInt64(0);
}
reader.Close();
SqlDataReader.GetSqlMoney()
Syntax
SqlMoney GetSqlMoney(Int32 i)
Description
The GetSqlMoney() method returns the value of a specified column as type SqlMoney.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlMoney value;
while(reader.Read()){
value = reader.GetSqlMoney(0);
}
reader.Close();
SqlDataReader.GetSqlSingle()
Syntax
SqlSingle GetSqlSingle(Int32 i)
Description
The GetSqlSingle() method returns the value of a specified column as type SqlSingle.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlSingle value;
while(reader.Read()){
value = reader.GetSqlSingle(0);
}
reader.Close();
SqlDataReader.GetSqlString()
Syntax
SqlString GetSqlString(Int32 i)
Description
The GetSqlString() method returns the value of a specified column as type SqlString.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlString value;
while(reader.Read()){
value = reader.GetSqlString(0);
}
reader.Close();
SqlDataReader.GetSqlValue()
Syntax
Object GetSqlValue(Int32 i)
Description
The GetSqlValue() method returns a value of type Object, by using its native SQL data type.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
System.Data.SqlTypes.SqlString value;
while(reader.Read()){
value = reader.GetSqlValue(0);
}
reader.Close();
SqlDataReader.GetString()
Syntax
String GetString(Int32 i)
Description
The GetString() method returns the value of a specified column as type String.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
string value;
while(reader.Read()){
value = reader.GetString(0);
}
reader.Close();
SqlDataReader.GetValue()
Syntax
Object GetValue(Int32 i)
Description
The GetValue() method returns the value of type Object, by using the Microsoft .NET framework types.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
string value;
while(reader.Read()){
value = reader.GetValue(0);
}
reader.Close();
SqlDataReader.IsDBNull()
Syntax
Boolean IsDBNull(Int32 i)
Description
The IsDBNull() method returns True if the specified column is null and False otherwise.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
string value;
while(reader.Read()){
if(reader.IsDBNull(0) != true)
value = reader.GetValue(0);
}
reader.Close();
SqlDataReader.NextResult()
Syntax
Boolean NextResult()
Description
The NextResult() method advances the data reader to the next record. It is used when reading the result of SQL batch statements.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("exec SqlBatch", conn);
SqlDataReader reader = cmd.ExecuteReader();
string value;
while(reader.NextResult()){
value = reader.GetValue(0);
}
reader.Close();
SqlDataReader.Read()
Syntax
Boolean Read()
Description
The Read() method advances the data reader to the next record. It returns True if there are more records and False otherwise.
Example
SqlConnection conn = new SqlConnection("Data Source=localhost;
User Id=sa; Password=; Initial Catalog=northwind");
conn.Open();
SqlCommand cmd = new SqlCommand("Select fieldname from aTable", conn);
SqlDataReader reader = cmd.ExecuteReader();
string value;
while(reader.Read()){
value = reader.GetValue(0);
}
reader.Close(); |