tags:

views:

285

answers:

4

Hi all,

I am looking for some decent ADO.NET helper utility class to allow me to query an ado.net datasource. I am not looking for anything too fancy, but it has to support transactions. Is there anything out there?

P.S. I know the data access block will do that but I was looking for something a bit more independent to other components. like a simple library or something

A: 

Use a simple ORM like SubSonic ?

Mischa Kroon
+2  A: 

It depends on your definitions of "nothing fancy" and "simple", but BLToolkit does a pretty decent job of abstracting away boilerplate ADO.NET code that you don't really want to write manually. For instance,

public abstract class PersonAccessor : DataAccessor<Person>
{
    [SqlQuery("SELECT * FROM Person WHERE PersonID = @id")]
    public abstract Person GetPersonByID(int @id);
}

Person person = DataAccessor.CreateInstance<PersonAccessor>.
    GetPersonByID(2);

will fetch a Person object from the DB in just -- see that? -- 5-6 lines of code.

As for DataSets, here's how:

        using (DbManager db = new DbManager())
        {
            DataSet ds = db
                .SetCommand("SELECT * FROM Person")
                .ExecuteDataSet();

            Assert.AreNotEqual(0, ds.Tables[0].Rows.Count);
        }

Adding parameters is as simple as calling a method on the DbManager.

And you shouldn't be really afraid of ORMs.

Anton Gogolev
This is fancier that what i have in mind. I would like to be able to do something.CommandType = Text; command.AddInParameter("foo", value); command.CommandText = "Select * From SomeTable"; helper.GetDataset(command, connectionString);Anything as simple as that? basically a very very light wrapper above the std ado.net objects and methods
Yannis
clarification: i only care to retrieve datasets from the db. i dont care for orm - in fact i dont want it
Yannis
i am not afraid of orms.....I actually use them quite heavily. but in this particular scenario an ORM isnt good enough. thanks
Yannis
A: 

If its just a light wrapper Why not just wrap the commands yourself?

eg:

    /// <summary>
    /// Executes a stored procedure or query, returns the number of rows effected.
    /// </summary>
    /// <param name="commandText"></param>
    /// <param name="commandType"></param>
    /// <param name="sqlParameters"></param>
    /// <param name="sqlTransaction"></param>
    /// <returns></returns>
    public static int ExecuteQuery(string commandText, CommandType commandType, List<SqlParameter> sqlParameters, SqlTransaction sqlTransaction)
    {
        if (sqlTransaction == null)
        {
            using (SqlConnection sqlConnection = new SqlConnection(GetConnectionString()))
            {
                sqlConnection.Open();
                using (SqlCommand sqlCommand = sqlConnection.CreateCommand())
                {
                    sqlCommand.CommandType = commandType;
                    sqlCommand.CommandText = commandText;
                    if (sqlParameters != null)
                    {
                        foreach (SqlParameter sqlParameter in sqlParameters)
                        {
                            sqlCommand.Parameters.Add(sqlParameter);
                        }
                    }
                    return sqlCommand.ExecuteNonQuery();
                }
            }
        }
        else
        {
            SqlCommand sqlCommand = new SqlCommand(commandText, sqlTransaction.Connection, sqlTransaction);
            sqlCommand.CommandType = commandType;
            foreach (SqlParameter sqlParameter in sqlParameters)
            {
                sqlCommand.Parameters.Add(sqlParameter);
            }
            return sqlCommand.ExecuteNonQuery();
        }
    }
Mark Redman
I'll let you create simpler overloads, implement the GetConectionString() method and wrap the other ones :-)
Mark Redman