List:MySQL and .NET« Previous MessageNext Message »
From:Fredrick Bartlett Date:October 6 2004 2:49pm
Subject:Re: Better way to get all columns in the DataReader
View as plain text  
Not really a better way for DataReader.  Don't forget to test for null...
     if(!Dr.IsDBNull(Dr.GetOrdinal("FirstName"))){
       MemberDetail.FirstName = Dr.GetString(Dr.GetOrdinal("FirstName"));
     }else{
       MemberDetail.FirstName = "";
     }
You might get better performance by Dr[0] etc.

----- Original Message ----- 
From: "Guy Platt" <guy@stripped>
To: <dotnet@stripped>
Sent: Wednesday, October 06, 2004 1:53 AM
Subject: Better way to get all columns in the DataReader


> There must a be a better way to get all the columns in a DataReader and
> map them to a class than the way I am doing it. Can anyone help me
> rewrite the following?
>
> Thanks
>    Guy
> P.S. The code has been edited (including removing a check to see whether
> the Dr returned any rows).
>
>
>     public class MemberDetail
>     {
>         public int MemberNrr;
>         public string FirstName;
>         public string LastName;
>         public string Password;
>         public string TelephoneNr;
>         public string MobileNr;
>         public string CancelDate;
>     }
>
> public static MemberDetail GetMemberDetails(int memberNr)
>         {
>             MySqlConnection myConn;
>             MySqlCommand myCmd = new MySqlCommand();
>             MySqlDataReader Dr = null;
>             myConn = new
>
MySqlConnection(ConfigurationSettings.AppSettings["MySqlConnectionString"]);
>
>             myConn.Open();
>             myCmd.Connection = myConn;
>             myCmd.CommandText = "SELECT * FROM ppmr_members WHERE
> ppmr_members.MembersNumber = " + memberid  ;
>             Dr = myCmd.ExecuteReader(CommandBehavior.CloseConnection);
>             Dr.Read();
>
>             MemberDetail myMemberDetail = new MemberDetail();
>
>             myMemberDetail.MembersNumber =
> Convert.ToInt32(Dr[Dr.GetOrdinal("MemberNr")]);
>             myMemberDetail.FirstName =
> Dr[Dr.GetOrdinal("FirstName")].ToString();
>             myMemberDetail.LastName =
> Dr[Dr.GetOrdinal("LastName")].ToString();
>             myMemberDetail.TelephoneNr =
> Dr[Dr.GetOrdinal("Telephone")].ToString();
>             myMemberDetail.MobileNr =
> Dr[Dr.GetOrdinal("Mobile")].ToString();
>             if (Dr[Dr.GetOrdinal("CancelDate")] != DBNull.Value)
>                 myMemberDetails.CancelDate =
> Dr[Dr.GetOrdinal("CancelDate")].ToString();
>             else
>                  myMemberDetail.CancelDate = String.Empty;
>
>
>             Dr.Close();
>             myConn.Close();
>             return myMemberDetail;
>         }
>
>
>


----------------------------------------------------------------------------
----


> -- 
> MySQL on .NET Mailing List
> For list archives: http://lists.mysql.com/dotnet
> To unsubscribe:
http://lists.mysql.com/dotnet?unsub=1

Thread
Return parameter from query.Asbjørn Konstad24 Sep
  • Re: Return parameter from query.Fredrick Bartlett26 Sep
  • RE: Return parameter from query.Reggie Burnett26 Sep
    • Better way to get all columns in the DataReaderGuy Platt6 Oct
      • RE: Better way to get all columns in the DataReaderJordan Sparks6 Oct
        • Re: Better way to get all columns in the DataReaderGuy Platt6 Oct
          • RE: Better way to get all columns in the DataReaderJordan Sparks6 Oct
          • Re: Better way to get all columns in the DataReaderBarry Zubel6 Oct
    • Re: Better way to get all columns in the DataReaderFredrick Bartlett6 Oct