示例#1
0
        public static DbContextSqlResult Error(string error)
        {
            DbContextSqlResult result = new DbContextSqlResult();

            result.Errors.Add(error);
            return(result);
        }
示例#2
0
        public DbContextSqlResult RunSql(HttpContext httpContext, string contextName, string sql, int maxRowsReads = 1000)
        {
            try {
                Type   dbContextType = GetDbContextTypes().FirstOrDefault(x => x.Name == contextName);
                object dbContext     = httpContext?.RequestServices?.GetService(dbContextType);
                if (dbContext == null)
                {
                    return(DbContextSqlResult.Error("Cannot find DbContext: " + contextName));
                }

                object          database        = Reflector.GetPropertyValue(dbContext, "Database");
                List <string>   columns         = new List <string>();
                List <object[]> rows            = new List <object[]>();
                int             recordsAffected = -1;
                var             relatoinDatabaseExtensionType = GetRelationalDatabaseExtensionType();
                using (var dbConnection = Reflector.InvokeStaticMethod(relatoinDatabaseExtensionType, "GetDbConnection", database) as IDisposable)
                {
                    Reflector.InvokeMethod(dbConnection, "Open");
                    using (var dbCommand = Reflector.InvokeMethod(dbConnection, "CreateCommand") as IDisposable)
                    {
                        Reflector.SetPropertyValue(dbCommand, "CommandText", sql);
                        using (var reader = Reflector.InvokeMethod(dbCommand, "ExecuteReader") as IDisposable)
                        {
                            while ((bool)Reflector.InvokeMethod(reader, "Read"))
                            {
                                if (columns.Count == 0)
                                {
                                    int fieldCount = (int)Reflector.GetPropertyValue(reader, "FieldCount");
                                    for (int i = 0; i < fieldCount; i++)
                                    {
                                        columns.Add(Reflector.InvokeMethod(reader, "GetName", i)?.ToString());
                                    }
                                }
                                object[] row = new object[columns.Count];
                                for (int i = 0; i < columns.Count; i++)
                                {
                                    row[i] = Reflector.InvokeMethod(reader, "GetValue", i);
                                }
                                rows.Add(row);
                                if (rows.Count >= maxRowsReads)
                                {
                                    break;
                                }
                            }
                            recordsAffected = (int)Reflector.GetPropertyValue(reader, "RecordsAffected");
                        }
                    }
                }
                return(new DbContextSqlResult
                {
                    Columns = columns,
                    Rows = rows,
                    AffectedRecords = recordsAffected
                });
            }
            catch (Exception ex)
            {
                return(DbContextSqlResult.Error(ex.GetAllMessages()));
            }
        }