List:MySQL and .NET« Previous MessageNext Message »
From:Guy Platt Date:October 6 2004 8:53am
Subject:Better way to get all columns in the DataReader
View as plain text  
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;
        }
   

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