T
Tony
Hello!
Here I have two versions that are almost identical. the only thing that is
different is the following rows.
This row exist in the first version
Console.WriteLine("{0}: {1}", rdr[intCustomerIDOrdinal],
rdr[intCompanyNameOrdinal]);
and this is in the second version
Console.WriteLine("{0}: {1}", rdr.GetString(intCustomerIDOrdinal),
rdr.GetString(intCompanyNameOrdinal));
As you can see both these columns CustomerID and CompanyName are of type
string. They are from the Northwind database. In the first version I write
out these by using the ordinal lookup and in the second I use strongly type
getters GetString.
I mean that if the columns have been value type like int and we have to do
boxing there would have been a difference but now when we have string which
are reference type there would not be any difference.
The reason I ask is that I read in a book named "Programming Microsoft
ado.net core reference by David Sceppa"
Here it says "The SqlDataReader class exposes Get metods for many basic .NET
data types - GetString, GetInt32,GetDateTime, and so on. These methods are
generally called the strongly typed getters. You can avoid the performance
penalty incurred by boxing and unboxing by calling the appropriate Get
method on the SqlDataReader. For example the CustomerID and CompanyName
columns contain string data. So we can use the GetString method of the
SqlDataReader to return the contens of those columns as a string, as shown
im the following code. Although the outpot appear the same, this code runs
faster than the previous code snippet.
Version 1
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string strSQL = "select CustomerID, CompanyName from Customers";
SqlCommand cmd = new SqlCommand(strSQL, con);
SqlDataReader rdr = cmd.ExecuteReader();
int intCustomerIDOrdinal, intCompanyNameOrdinal;
intCustomerIDOrdinal = rdr.GetOrdinal("CustomerID");
intCompanyNameOrdinal = rdr.GetOrdinal("CompanyName");
while (rdr.Read())
Console.WriteLine("{0}: {1}", rdr[intCustomerIDOrdinal],
rdr[intCompanyNameOrdinal]);
rdr.Close();
Version 2
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string strSQL = "select CustomerID, CompanyName from Customers";
SqlCommand cmd = new SqlCommand(strSQL, con);
SqlDataReader rdr = cmd.ExecuteReader();
int intCustomerIDOrdinal, intCompanyNameOrdinal;
intCustomerIDOrdinal = rdr.GetOrdinal("CustomerID");
intCompanyNameOrdinal = rdr.GetOrdinal("CompanyName");
while (rdr.Read())
Console.WriteLine("{0}: {1}",
rdr.GetString(intCustomerIDOrdinal),
rdr.GetString(intCompanyNameOrdinal));
rdr.Close();
//Tony
Here I have two versions that are almost identical. the only thing that is
different is the following rows.
This row exist in the first version
Console.WriteLine("{0}: {1}", rdr[intCustomerIDOrdinal],
rdr[intCompanyNameOrdinal]);
and this is in the second version
Console.WriteLine("{0}: {1}", rdr.GetString(intCustomerIDOrdinal),
rdr.GetString(intCompanyNameOrdinal));
As you can see both these columns CustomerID and CompanyName are of type
string. They are from the Northwind database. In the first version I write
out these by using the ordinal lookup and in the second I use strongly type
getters GetString.
I mean that if the columns have been value type like int and we have to do
boxing there would have been a difference but now when we have string which
are reference type there would not be any difference.
The reason I ask is that I read in a book named "Programming Microsoft
ado.net core reference by David Sceppa"
Here it says "The SqlDataReader class exposes Get metods for many basic .NET
data types - GetString, GetInt32,GetDateTime, and so on. These methods are
generally called the strongly typed getters. You can avoid the performance
penalty incurred by boxing and unboxing by calling the appropriate Get
method on the SqlDataReader. For example the CustomerID and CompanyName
columns contain string data. So we can use the GetString method of the
SqlDataReader to return the contens of those columns as a string, as shown
im the following code. Although the outpot appear the same, this code runs
faster than the previous code snippet.
Version 1
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string strSQL = "select CustomerID, CompanyName from Customers";
SqlCommand cmd = new SqlCommand(strSQL, con);
SqlDataReader rdr = cmd.ExecuteReader();
int intCustomerIDOrdinal, intCompanyNameOrdinal;
intCustomerIDOrdinal = rdr.GetOrdinal("CustomerID");
intCompanyNameOrdinal = rdr.GetOrdinal("CompanyName");
while (rdr.Read())
Console.WriteLine("{0}: {1}", rdr[intCustomerIDOrdinal],
rdr[intCompanyNameOrdinal]);
rdr.Close();
Version 2
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string strSQL = "select CustomerID, CompanyName from Customers";
SqlCommand cmd = new SqlCommand(strSQL, con);
SqlDataReader rdr = cmd.ExecuteReader();
int intCustomerIDOrdinal, intCompanyNameOrdinal;
intCustomerIDOrdinal = rdr.GetOrdinal("CustomerID");
intCompanyNameOrdinal = rdr.GetOrdinal("CompanyName");
while (rdr.Read())
Console.WriteLine("{0}: {1}",
rdr.GetString(intCustomerIDOrdinal),
rdr.GetString(intCompanyNameOrdinal));
rdr.Close();
//Tony