Ejemplo n.º 1
0
        private static T Execute <T>(this IDbConnection conn, string cmdText, CommandType cmdType, int?cmdTimeout, IDbTransaction tran, DataParam[] dbParams, Func <IDbCommand, T> action)
        {
            bool flag = false;
            T    result;

            try
            {
                using (IDbCommand dbCommand = conn.CreateCommand())
                {
                    DbConnectionExtension.SetupCommand(dbCommand, cmdText, cmdType, cmdTimeout, dbParams, tran);
                    if (conn.State != ConnectionState.Open)
                    {
                        flag = true;
                        conn.Open();
                    }
                    result = action(dbCommand);
                }
            }
            finally
            {
                if (flag && conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return(result);
        }