C# 填充接口
有人能告诉我如何在界面C# 填充接口,c#,interface,implements,C#,Interface,Implements,有人能告诉我如何在界面IAccount中填充字段吗?我在x.Add(新IAccount…)中出错。 public class IPersonRepo : IAccount { string connectionstring = @"Server=SLI002/SQLEXPRESS;Database=atengturonDB;Trusted_Connection=true;"; public int AccountsID { get {
IAccount
中填充字段吗?我在x.Add(新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();
}
}
}
公共类IPersonRepo:IAccount
{
字符串连接字符串=@“服务器=SLI002/SQLEXPRESS;数据库=atengturonDB;可信的_连接=true;”;
公共int帐户SID
{
收到
{
抛出新的NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}
公共字节[]帐户用户名
{
收到
{
抛出新的NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}
公共字节[]帐户密码
{
收到
{
抛出新的NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}
公共字节[]AccountSalt
{
收到
{
抛出新的NotImplementedException();
}
设置
{
抛出新的NotImplementedException();
}
}
公共帐户()
{
SqlConnection conn=新的SqlConnection(connectionstring);
使用(SqlCommand comm=newsqlcommand())
{
列表x=新列表();
通信连接=连接;
comm.CommandText=“从Accounts中选择AccountsID、AccountsUserName、AccountsPassword、AccountsSalt”;
comm.CommandType=CommandType.Text;
SqlDataReader=null;
conn.Open();
comm.ExecuteNonQuery();
reader=comm.ExecuteReader();
while(reader.Read())
{
x、 添加(新的IAccount)
{
AccountsID=(int)读取器[“AccountsID”],
AccountUserName=(字节[])读取器[“AccountsUserName”],
AccountPassword=(字节[])读取器[“AccountsPassword”],
AccountsSalt=(字节[])读取器[“AccountsSalt”]
});
}
康涅狄格州关闭();
}
}
}
请将IPersonRepo
重命名为PersonRepo
,前缀I
表示接口,但显然它是类。第二,它看起来不像repo(=存储库),但像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或实体框架。可以帮助您,但这是您的电话:)首先,在决定类名时,切勿使用
I
前缀。这不是一个编译错误,但非常令人困惑,因为惯例是使用I作为接口名称的前缀。因此,您的类应该被称为
PersonRepo
,而不是IPersonRepo
(但是,您可以将以
I
开头的类命名为like(Ice
),只是不要将I
用作前缀)
其次,不能实例化接口。您可以使用接口类型的变量,但要实例化实际的类:
IAccount MyAccount=new PersonRepo()
您无法实例化接口。将IPersonRepo
重命名为class PersonAccount:IAccount
-然后x.Add如何(新的PersonAccount
?如果您有公共类IPersonRepo
或公共类IAccount
您没有接口
,但是类
谢谢StuartLC,它工作了!谢谢!现在我明白了!好的,先生,感谢这些新信息,我正在努力理解这些接口和repo情景