为什么我收到错误错误1不一致的可访问性:返回类型?

时间:2015-09-05 21:01:48

标签: c# ado.net

这个项目应该有一个联系人类和地址类,我从数据文件中提取信息

namespace Week1.DataAccess
{
    public class AddressBookRepo
    {
        private string ConnectionString
        {
            get
            {
                return ConfigurationManager
                    .ConnectionStrings["AddressbookConnectionString"]
                    .ConnectionString;
            }
        }

        public List<Contact> GetAllContacts()
        {
            var contactList = new List<Contact>();
            using (var conn = new SqlConnection(ConnectionString))
            {
                using (var command = new SqlCommand())
                {
                    command.Connection = conn;
                    command.CommandText = "SELECT * FROM CONTACT";
                    conn.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var contact = GetContactFromDataReader(reader);
                            contactList.Add(contact);
                        }
                    }
                }

            }

            return contactList;
        }

        public Contact GetContactById(int id)
        {
            Contact contact = null;

            using (var conn = new SqlConnection(ConnectionString))
            {
                using (var command = new SqlCommand())
                {
                    command.Connection = conn;
                    conn.Open();
                    command.CommandText = 
                                @"SELECT * 
                                FROM CONTACT 
                                WHERE ContactID = @ContactId";
                    command.Parameters.AddWithValue("ContactID", id);

                    using (var reader = command.ExecuteReader())
                    {
                        reader.Read();
                        contact = GetContactFromDataReader(reader);
                    }
                }
            }
            return contact;
        }

        // 2.) create a GetAllAddresses method
        public List<Address> GetAllAddresses() //i am getting error here
        {
            var addressList = new List<Address>();
            using (var conn = new SqlConnection(ConnectionString))
            {
                using (var command = new SqlCommand())
                {
                    command.Connection = conn;
                    command.CommandText = "SELECT * FROM ADDRESS";
                    conn.Open();
                    using (var reader = command.ExecuteReader())
                    {
                        while (reader.Read())
                        {
                            var address = GetAddressFromDataReader(reader);
                            addressList.Add(address);
                        }
                    }
                }

            }

            return addressList;
        }

        // 3.) create a GetAddressById method
        public Address GetAddressById(int id)// I am getting error here
        {
            Address address = null;

            using (var conn = new SqlConnection(ConnectionString))
            {
                using (var command = new SqlCommand())
                {
                    command.Connection = conn;
                    conn.Open();
                    command.CommandText =
                                @"SELECT * 
                                FROM ADDRESS 
                                WHERE ADDRESSID = @AddressId";
                    command.Parameters.AddWithValue("addressID", id);

                    using (var reader = command.ExecuteReader())
                    {
                        reader.Read();
                        address = GetAddressFromDataReader(reader);
                    }
                }
            }
            return address;
        }


        // 4.) create a method to GetAddressesByState (extra credit)

        private static Contact GetContactFromDataReader(SqlDataReader reader)
        {
            var contact = new Contact
            {
                ContactId = Convert.ToInt32(reader["ContactID"]),
                FirstName = Convert.ToString(reader["FirstName"]).Trim(),
                LastName = Convert.ToString(reader["LastName"]).Trim(),
                Title = reader["Title"].ToString().Trim(),
                AddDate = (DateTime)reader["AddDate"],
                ModifiedDate = (DateTime)reader["ModifiedDate"]
            };
            return contact;
        }

        private static Address GetAddressFromDataReader(SqlDataReader reader)
        {
            var address = new Address
            {
                AddressId = Convert.ToInt32(reader["addressID"]),
                Street1 = Convert.ToString(reader["Street1"]),
                Street2 = Convert.ToString(reader["Street2"]),
                City = Convert.ToString(reader["City"]),
                StateProvince = Convert.ToString(reader["StateProvince"]),
                CountryRegion = Convert.ToString(reader["CountryRegion"]),
                PostalCode = Convert.ToString(reader["PostalCode"]),
                AddressType = Convert.ToString(reader["AddressType"]),
                ContactId = Convert.ToInt32(reader["ContactID"]),
                ModifiedDate = (DateTime)reader["ModifiedDate"]
            };
            return address;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您的课程(可能是地址广告/联系人)可能不公开,无法与其所用方法的可访问性相匹配