Exemplo n.º 1
0
        public static IDataReader RunOneSqlCommandReader(this DbConnection conn, ISqlDialect dialect, string sql, DbTransaction tran, int?timeout, IInvoker invoker)
        {
            if (sql.StartsWith("@getschema"))
            {
                string[] items  = sql.Split(' ');
                string   entity = items[1].ToLower().Trim();
                if (entity == "databases")
                {
                    return(new DataTableReader(conn.GetSchema("Databases")));
                }
                if (entity == "tables")
                {
                    return(new DataTableReader(conn.GetSchema("Tables")));
                }
                if (entity == "columns")
                {
                    return(new DataTableReader(conn.GetSchema("Columns", new string[] { null, null, items[2].Trim() }).SelectNewTable("1=1", "ORDINAL_POSITION ASC")));
                }
            }
            DbCommand c = conn.CreateCommand();

            c.Connection = conn;
            if (timeout != null)
            {
                c.CommandTimeout = timeout.Value;
            }
            c.CommandText = sql;
            if (tran != null)
            {
                c.Transaction = tran;
            }
            Logging.Debug("Executing SQL:" + sql);
            IDisposable canc = null;

            if (invoker != null)
            {
                canc = invoker.AddOnCancel(c.Cancel);
            }
            try
            {
                return(new CommandDataReader(c.ExecuteReader(), c, canc));
            }
            catch (Exception err)
            {
                err.Data["sql"] = c.CommandText;
                if (c.Connection != null)
                {
                    c.Connection.FillInfo(err.Data);
                }
                if (dialect != null)
                {
                    dialect.FillInfo(err.Data);
                }
                throw;
            }
        }