Fill variable with different types

Asked

Viewed 88 times

1

I have many fields to pick up so it is very bad to make a function for each connection and they are uncharted banks so using something like the Entity Framework is also not feasible

if (1 == 1) {
    OdbcConnection connection = new OdbcConnection(con);
    OdbcCommand cmd = connection.CreateCommand();
} else {
    MySqlConnection connection = new MySqlConnection(con);
    MySqlCommand cmd = connection.CreateCommand();
}
cmd.CommandText = "";
connection.Open();

The point is how to fill the same variable with 2 different object types, so I can change only the connection method without having to do the whole query again.


I found the solution, use dynamic, so it accepts different objects and can access their functions

dynamic sqlConn = null;
dynamic cmd = null;
if (1 == 1) {
    connection = new OdbcConnection(con);
    cmd = connection.CreateCommand();
} else {
    connection = new MySqlConnection(con);
    cmd = connection.CreateCommand();
}
cmd.CommandText = "";
connection.Open();

1 answer

1


You can’t do it like this.

I don’t know if it was a mistake (it seems not) or was purposeful, but the class OdbcConnection does not derive from one that can be generalized. If it was purposeful then it must have a reason, even if not explicit (there are indications in the documentation), and it must be different even. It even derives from something too generic (Component) which cannot be used here, it would have to be something more specific a little, see below.

Already the MySqlConnection derives from DbConnection that seems appropriate. If the other class derived from it too, and intuitively I think which should, but also has indications that it should not, so it would be easy to solve with a more generic type object, the DbConnection.

But it doesn’t have that and it seems to have a good reason, so the right thing is not to generalize, it has to do everything separately even. They are completely different logics, it is not correct to try to join the two, however tempting.

ODBC has several limitations, works the connection in a different way and needs care that a normal connection does not need, so they probably made it different and did not derive from DbConnection, that is, from what is in the documentation gives the idea that it is a mistake to do what you are trying to.

I’m not even getting into the merits if this is all right. Should we handle an ODBC database be used? Are you sure? This is quite complicated to work right, even more trying to use the same logic for him and for Mysql. Unless you’re not using the same logic, then it wouldn’t make sense to try to join the connection.

The solution seems to be different, but without a bigger context you can’t even begin to help. It seems to be a XY problem.

Not to mention that most of the codes, even simple, of connection that I see posted here have problems and unnecessary complications, I can not say that this is the case for the lack of context, but may have other problems, even the unnecessary use of ODBC.

And don’t use dynamic to make a gambit. It may work, but it’s not right. This command was created for cases where you have no control over the composition of the object. In this case you have control. Less still start something with null, almost in all cases this is a sign of error. It is creating a problem to fix the real problem. It is working by coincidence.

Fiat 147 todo detonado andando pelas ruas

  • These functions I use take more than 50 fields of the table, The tables have the same structure only they are in different banks, make 1 function for each bank would q a modification in something need to be done in the 3 functions and would leave the code much bigger, so I used this solution.

  • 1

    Not creating the right abstraction, that is, it is an XY problem.

  • I don’t believe this is considered an answer to people who are having the same problem, but since I can’t put the question as an answer I’ll mark yours to close the topic.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.