Rather than maintaining a few different database access layers for OleDbConnection, MySqlConnection, OdbcConnection, and Db2Connection, I was trying to figure out a way to use generics. However, I get an error when I try to compile the code, I get errors when I try to access the class’s methods or properties.
public class DatabaseConnector<CONNECTION> {
private CONNECTION connection = default(CONNECTION);
public bool IsConnected {
get {
return (
this.connection != null &&
// error on connection.State on the following two lines
this.connection.State != System.Data.ConnectionState.Closed &&
this.connection.State != System.Data.ConnectionState.Broken
);
}
}
}
Is there any way around this? Or perhaps another class that can handle the many versions?
You’re looking for constraints: