.NET SqlDataReader Item[] vs. GetString(GetOrdinal())?

前端 未结 5 1222
感情败类
感情败类 2020-12-18 22:28

Using the SqlDataReader class, what, if any, are the functional differences between:

(string) dataReader[\"MyFieldName\"];

and

5条回答
  •  梦毁少年i
    2020-12-18 22:36

    //Well, we want to avoid the null exception issue entirely.
    //Let's check for null first, before we try to use the value.
    
    if( !dataReader.IsDBNull(dataReader.GetOrdinal("MyFieldName")))
    {
    //Store my data or use the value
    string mystring=dataReader.GetString(dataReader.GetOrdinal("MyFieldName"));
    }
    

    Serendipity is a wonderful discovery method.

提交回复
热议问题