填充界面

时间:2015-05-25 08:51:43

标签: c# interface implements

有谁能告诉我如何填充界面IAccount中的字段?我在x.Add(new IAccount ...

中遇到错误
public class IPersonRepo : IAccount
{
    string connectionstring = @"Server=SLI002/SQLEXPRESS;Database=atengturonDB;Trusted_Connection=true;";
    public int AccountsID
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountUserName
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountPassword
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public byte[] AccountSalt
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }
    public void getAccount()
    {
        SqlConnection conn = new SqlConnection(connectionstring);
        using (SqlCommand comm = new SqlCommand())
        {
            List<IAccount> x = new List<IAccount>();
            comm.Connection = conn;
            comm.CommandText = "Select AccountsID,AccountsUserName,AccountsPassword,AccountsSalt from Accounts";
            comm.CommandType = CommandType.Text;
            SqlDataReader reader = null;
            conn.Open();
            comm.ExecuteNonQuery();
            reader = comm.ExecuteReader();
            while (reader.Read())
            {
                x.Add(new IAccount
              {
                  AccountsID = (int)reader["AccountsID"],
                  AccountUserName = (byte[])reader["AccountsUserName"],
                  AccountPassword = (byte[])reader["AccountsPassword"],
                  AccountSalt = (byte[])reader["AccountsSalt"]
              });

            }
            conn.Close();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

请将IPersonRepo重命名为PersonRepo,前缀I表示接口,但显然是类。其次,它看起来不像repo(= repository),但像Person(但这是有争议的...... :))

第三,您正在尝试创建接口 - 但您必须实现该接口的实例类:

//x.Add(new IAccount
//x.Add(new IPersonRepo
//x.Add(new PersonRepo
x.Add(new Person
          {
              AccountsID = (int)reader["AccountsID"],
              AccountUserName = (byte[])reader["AccountsUserName"],
              AccountPassword = (byte[])reader["AccountsPassword"],
              AccountSalt = (byte[])reader["AccountsSalt"]
          });

第四个也是最后一个,也许你应该看看任何ORM,比如NHibernate或Entity Framework。可以帮到你,但是你的电话:)

答案 1 :(得分:0)

首先,决定使用类名时不要使用I前缀。这不是编译错误,但它很混乱,因为约定是使用I作为接口名称的前缀。
因此,您的课程应该被称为PersonRepo,而不是IPersonRepo (但是,您可以将以I开头的类命名为(Ice),只是不要使用I作为前缀)

第二,您无法实例化界面。您可以使用接口类型的变量,但实例化实际的类:IAccount MyAccount = new PersonRepo();