2

Can anyone point me towards a current library that provides basic wrapping of ADO.NET functionality? I'm looking for something along the lines of the old SqlHelper class and am not really interested in using the Data Access Application Block (as it's a bit of overkill for my simple needs). What is everyone using for working with ADO.NET directly these days?

Update:

I should note that I'm already working with an ORM (Nhibernate); I've just run up against a situation that requires raw ADO.NET calls - so no need to suggest using an ORM instead of working with ADO.NET

DanP
  • 6,310
  • 4
  • 40
  • 68

4 Answers4

2

I've written my own little helper library (one .cs file) here: https://github.com/jhgbrt/yadal/blob/master/Net.Code.ADONet.SingleFile/netstandard/Db.cs

You can find the non-joined version, tests, and readme here: https://github.com/jhgbrt/yadal

jeroenh
  • 26,362
  • 10
  • 73
  • 104
2

Dan, this is a class that I have built up over a few years. I use ADO.NET extensivly. It supports simple things like Fill, NonQuery, Scalar, but also getting a schema, transactions, bulk inserts, and more.

DataAdapter (VisualStudio 2010 solution)

Let me know if you need any more help using this (note: I removed some links to other objects to post this for you, so if it's broken, just let me know).

Brad
  • 15,361
  • 6
  • 36
  • 57
  • thanks for sharing...but even this is a little heavier than I'm looking for..and it seems to contain a lot of 'legacy' features (eg. use of array lists), etc. – DanP Sep 23 '10 at 19:09
  • Yeah, I see that on the GetReader method. A few months ago I updated the Fill, NonQuery, and Scalar methods to take params IDbParameter[] parameters instead of the ArrayList, but I didn't update everything. – Brad Sep 23 '10 at 19:59
  • @Dan, What do you think makes it so 'heavy'? – Brad Sep 23 '10 at 20:05
  • My needs are really simple; infact I'm starting to think I might be able to get away with just adding some extension methods to IDataReader to make my life easier. I'm always hesitant about adding large chunks of functionality to my codebase that aren't mantained elsewhere (eg. active open source or commercial package). Not a knock at all against what you've provided - just not what I'm looking for exactly. – DanP Sep 23 '10 at 22:34
1

I ended up going with Fluent Ado.net for this; great little library for doing the simple out-of-band ado.net stuff that pops up every now and then.

DanP
  • 6,310
  • 4
  • 40
  • 68
0

Hope it helpful:

public static class DbHelper {

        public static IDbCommand CreateCommand(this IDbConnection conn, string sql, params object[] args) {
            if (!(conn is SqlConnection))
                throw new NotSupportedException();
            var command = (SqlCommand)conn.CreateCommand();
            try { 
                var paramterNames = new List<string>(args.Length);
                for (int i = 0; i < args.Length; i++) {
                    string name = "@p" + i;
                    command.Parameters.AddWithValue(name, args[i]);
                    paramterNames.Add(name);
                }
                command.CommandText = string.Format(sql, paramterNames.ToArray());
            }
            catch (Exception) {
                if (command != null)
                    command.Dispose();
                throw;
            }
            return command;
        }
}
Thong Doan
  • 187
  • 2
  • 7