Exemplo n.º 1
0
    public static DetailedUserInfo getDetailedUserInfo(int user_id)
    {
        DetailedUserInfo userinfo = new DetailedUserInfo();

        try
        {
            using (SqlConnection con = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand("select * from Users  where ID=@id", con))
                {
                    con.Open();
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.Add("@id", SqlDbType.Int).Value = user_id;
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        if (reader.HasRows)
                        {
                            if (reader.Read())
                            {
                                PropertyInfo[] propertys = userinfo.GetType().GetProperties();
                                foreach (PropertyInfo info in propertys)
                                {
                                    info.SetValue(userinfo, Convert.ChangeType(reader[info.Name], info.PropertyType), null);
                                    //propertyInfo.SetValue(ship, Convert.ChangeType(value, propertyInfo.PropertyType), null);
                                }
                            }
                        }
                    }


                    con.Close();
                }
            }
        }
        catch (Exception ex)
        {
            // throw ex;
            // return 0;
        }
        return(userinfo);
    }