Esempio n. 1
0
        private void ExecuteSql(SqlScriptReader sqlReader, ExecutionContext context)
        {
            var connectionInfo = new ConnectionInfo
            {
                ConnectionName     = (string)context.ResolveVariable(ConnectionName),
                DataSource         = (string)context.ResolveVariable(DataSource),
                InitialCatalog     = InitialCatalog,
                InitialCatalogName = (string)context.ResolveVariable(InitialCatalogName),
                UserName           = (string)context.ResolveVariable(UserName),
                Password           = (string)context.ResolveVariable(Password)
            };
            var connectionString = MsSqlDataContext.GetConnectionString(connectionInfo, context.ConnectionStrings)
                                   ?? context.ConnectionStrings.Repository;

            while (sqlReader.ReadScript())
            {
                var script = sqlReader.Script;

                var sb = new StringBuilder();

                //TODO: [DIREF] get options from DI through constructor
                using (var ctx = new MsSqlDataContext(connectionString, DataOptions.GetLegacyConfiguration(), CancellationToken.None))
                {
                    ctx.ExecuteReaderAsync(script, async(reader, cancel) =>
                    {
                        do
                        {
                            if (reader.HasRows)
                            {
                                var first = true;
                                while (await reader.ReadAsync(cancel).ConfigureAwait(false))
                                {
                                    if (first)
                                    {
                                        for (int i = 0; i < reader.FieldCount; i++)
                                        {
                                            sb.Append(reader.GetName(i)).Append("\t");
                                        }
                                        Logger.LogMessage(sb.ToString());
                                        sb.Clear();
                                        first = false;
                                    }
                                    for (int i = 0; i < reader.FieldCount; i++)
                                    {
                                        sb.Append(reader[i]).Append("\t");
                                    }
                                    Logger.LogMessage(sb.ToString());
                                    sb.Clear();
                                }
                            }
                        } while (await reader.NextResultAsync(cancel).ConfigureAwait(false));
                        return(Task.FromResult(0));
                    }).GetAwaiter().GetResult();
                }
            }
            Logger.LogMessage("Script is successfully executed.");
        }
Esempio n. 2
0
        private void ExecuteSql(SqlScriptReader sqlReader, ExecutionContext context)
        {
            while (sqlReader.ReadScript())
            {
                var script = sqlReader.Script;

                var sb = new StringBuilder();
                using (var proc = DataProvider.CreateDataProcedure(script, ConnectionName, InitialCatalog))
                {
                    proc.CommandType = CommandType.Text;

                    using (var reader = proc.ExecuteReader())
                    {
                        do
                        {
                            if (reader.HasRows)
                            {
                                var first = true;
                                while (reader.Read())
                                {
                                    if (first)
                                    {
                                        for (int i = 0; i < reader.FieldCount; i++)
                                        {
                                            sb.Append(reader.GetName(i)).Append("\t");
                                        }
                                        Logger.LogMessage(sb.ToString());
                                        sb.Clear();
                                        first = false;
                                    }
                                    for (int i = 0; i < reader.FieldCount; i++)
                                    {
                                        sb.Append(reader[i]).Append("\t");
                                    }
                                    Logger.LogMessage(sb.ToString());
                                    sb.Clear();
                                }
                            }
                            //Logger.LogMessage("---------------------");
                        } while (reader.NextResult());
                    }
                }
            }
            Logger.LogMessage("Script is successfully executed.");
        }