Question

In my project, I have a DBAdapter class that deals with database queries.

public class DBAdapter
{
    private OleDbConnection _connection;

    private void _Connect()
    {
        this._connection = new OleDbConnection();
        _connection.ConnectionString = ConfigurationManager.AppSettings["Accessconnection"];
        _connection.Open();
    }

    private void _Disconnect()
    {
        _connection.Close();
    }


    public DataTable Select(string query, OleDbParameterCollection parameters = null)
    {

        OleDbCommand cmd = new OleDbCommand();
        OleDbDataAdapter dataAdapter = new OleDbDataAdapter();
        DataSet dataSet = new DataSet();
        DataTable dataTable = new DataTable();
        query = "SELECT " + query;

        if (parameters != null)
        {
            foreach (OleDbParameter parameter in parameters)
            {
                cmd.Parameters.Add(parameter);
            }
        }

        this._Connect();
        cmd.Connection = _connection;
        cmd.CommandText = query;

        if (parameters != null)  {
            cmd.Prepare();
        }
        dataAdapter.SelectCommand = cmd;
        dataAdapter.Fill(dataSet, "results");
        this._Disconnect();

        dataTable = dataSet.Tables["results"];
        return dataTable;
    }

}

In order to perform prepared queries, the Select method has an optionnal OleDBParameterCollection parameter. Then, I have multiple Mappers for each domain object in my project, for example UserMapper, that use DataAdapter class to run queries (for example find user by id).

    public class UserMapper : DataMapperAbstract
        {

            public User findByID(int id)
            {
                User user = new User()
                string query = "* FROM USER WHERE idUser = ?";
                OleDbParameterCollection parameters = new OleDbParameterCollection();
                parameters.Add(new OleDbParameter("idUser", OleDbType.Integer).Value = id);

                // Prepared query
                DataTable results = adapter.Select(query, parameters);
                this._populateData(user, results.Rows[0]);
                return user;
            }
    }

Unfortunately, I have an error at this line

   OleDbParameterCollection parameters = new OleDbParameterCollection();

VS says that the type OleDbParameterCollection has no constructor defined, and I don't really understand what is the problem here. Maybe I don't have rights to instantiate OleDbParameterCollection, but in that case, how should I pass a collection of parameters to my DBAdapter's method ?

Was it helpful?

Solution

OleDbParameterCollection doesn't expose public constructor we can access. It doesn't meant to be used that way so, simply change your method parameter to accept list of OleDbParameter instead of OleDbParameterCollection :

public DataTable Select(string query, List<OleDbParameter> parameters = null)
{
}

Then use it accordingly :

List<OleDbParameter> parameters = new List<OleDbParameter>();
parameters.Add(new OleDbParameter("idUser", OleDbType.Integer){ Value = id });

// Prepared query
DataTable results = adapter.Select(query, parameters);

OTHER TIPS

OleDbParameterCollection has no public constructors.

When the OleDbDataAdapter object is instantiated it automatically creates an OleDbParameterCollection object for you within the SelectCommand object.

Here is some code from MSDN that I found:

OleDbDataAdapter adapter =
new OleDbDataAdapter(queryString, connection);
// Set the parameters.
adapter.SelectCommand.Parameters.Add(
        "@CategoryName", OleDbType.VarChar, 80).Value = "toasters";

Here is the link: http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbparametercollection(v=vs.110).aspx

You may need to modify the way you run the query a little bit. But, it looks like you've already got an adapter object in there, so it shouldn't be too hard.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top