Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Ultimately it depends on the particular implementation of <code>System.Data.IDataReader</code> you are referring to, but let's assume you mean <code>System.Data.SqlClient.SqlDataReader</code>.</p> <p>In this case, <code>reader[i]</code> and <code>reader.GetValue(i)</code> do <em>exactly</em> the same thing. In fact, <code>reader[i]</code> internally calls <code>reader.GetValue(i)</code>. (You can see this by disassembling the code using a tool like <a href="http://wiki.sharpdevelop.net/ILSpy.ashx">ILSpy</a>.). Both these members return the value in the i-th column of the current row and will return successfully regardless of the type of the value (the return type is <code>object</code>). The documentation for both of these members is a little misleading and could be worded in a better way. You can use either of these members wherever you like, just pick the one which you feel reads better.</p> <p>Whereas <code>reader.GetString(i)</code> is specifically designed to return the value contained in the i-th column of the current row as a <code>string</code>. If the value in that column is not a <code>string</code>, an <code>InvalidCastException</code> will be thrown. Use this method when you are sure that the value is a <code>string</code> and <code>string</code> is the type that you want to work with. This will make your code more concise since you won't have to perform a cast.</p> <p>In your specific case, the value in the i-th column of the current row must be of type <code>string</code>, which is why the return value of all three members is identical.</p> <p>If written correctly, other implementations of <code>System.Data.IDataReader</code> should behave in the same way.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload