2

In short I am writing a class handler to handle to database integration of some software I am writing for myself, however as there is not always a connection to the remote database I thought I would use SQLCE to create a local database buffer so when a connection is made the changes can be synchronized.

So far it is going well except for the parameters. The function I am looking to call is shown below however this function is complaining about invalid arguments.

public Object run(string query, List<Object> dbparams = null)
    {
        if (MyDB.isConnected())
        {
            return MyDB.run(query, dbparams);
        }
        else
        {
            SqlCeCommand sql = _OfflineConnection.CreateCommand();
            sql.CommandText = query;
            if (dbparams.Count > 0)
            {
                sql.Parameters.AddRange(dbparams.ToArray());
            }
            return sql;
        }
    }

MyDB.run is the exact same code as in the else statement except for mysql, the line that it is moaning about is the return mydb.run as the mydb class is expecting the dbparams list to be of mysqlparameters.

Does anyone know how I can achieve this? I attempted to use LINQ to do a convert but that failed miserably.

EDIT

At present I have the following working but I am sure there is a cleaner solution

public Object run(string query, List<Object> dbparams = null)
    {
        if (MyDB.isConnected())
        {
            List<MySqlParameter> mydbparams = null;
            for (int i = 0; i < dbparams.Count; i++)
            {
                mydbparams.Add((MySqlParameter)dbparams[i]);
            }
            return MyDB.run(query, mydbparams);
        }
        else
        {
            SqlCeCommand sql = _OfflineConnection.CreateCommand();
            sql.CommandText = query;
            if (dbparams.Count > 0)
            {
                sql.Parameters.AddRange(dbparams.ToArray());
            }
            return sql;
        }
    }
2
  • I guess in the else branch you should create and open the SqlCeConnection to the sdf file. Maybe this implemented somewhere inside _OfflineConnection, but this is not clear. Commented Jun 11, 2012 at 20:19
  • @kol the issue here is only this part of the code, all other handles are already covered Commented Jun 11, 2012 at 20:21

1 Answer 1

2

A bit cleaner solution would be

mydbparams = dbparams.Cast<MySqlParameters>().ToList();

Also, you should check for and handle the null condition of dbparams.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Eren, I now have another issue where as I can't use the using () statement to handle to code neatly :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.