这应该不难回答,但我非常绝望和困惑。我制作了从数据库执行读取查询的接口
interface IDatabase
{
DataTable ExecuteReaderCommand(IDbCommand command);
IDbCommand GetNewCommand();
}
接下来,我在接口上面创建了两个不同的类实现。
class MysqlDatabase : IDatabase
{
public DataTable ExecuteReaderCommand(MySqlCommand command)
{
DataTable dt = new DataTable();
// ... read db
return dt;
}
public MySqlCommand GetNewCommand()
{
cnn.Open();
return cnn.CreateCommand();
}
}
和
class SQLiteDatabase : IDatabase
{
String dbConnection;
SQLiteConnection cnn;
public DataTable ExecuteReaderCommand(SQLiteCommand command)
{
DataTable dt = new DataTable();
// ... read db
return dt;
}
public SQLiteCommand GetNewCommand()
{
cnn.Open();
return cnn.CreateCommand();
}
}
但是我收到错误,这些类没有实现 IDatabase 接口:
MysqlDatabase does not implement interface member 'Database.GetNewCommand()'
MysqlDatabase.GetNewCommand() cannot implement 'Database.GetNewCommand()' because it does not have the matching return type of 'System.Data.IDbCommand'.
SQLiteDatabase does not implement interface member Database.ExecuteReaderCommand(System.Data.IDbCommand)
SQLiteDatabase does not implement interface member 'Database.GetNewCommand()'. SQLiteDatabase.GetNewCommand() cannot implement Database.GetNewCommand() because it does not have the matching return type of 'System.Data.IDbCommand'.
当我查看它们时SQLiteCommand
,MySqlCommand
它们都实现了IDbCommand
.
如何在通用接口下使用这些类,以便轻松切换它们?
我非常感谢任何答案。