private static void UpdateProgress(SqlServer sqlServer, int rowsAdded, int rowsToAdd)
 {
     sqlServer.Progress = rowsAdded * 100 / rowsToAdd;
 }
        public static void StartGenerator(SqlServer sqlServer)
        {
            var rowsAdded = 0;
            var instance  = GetSqlInstanceNameFromService(sqlServer.ServerCredentials.Ip, sqlServer.ServerCredentials.Username, sqlServer.ServerCredentials.Password);
            var builder   = new SqlConnectionStringBuilder
            {
                DataSource         = sqlServer.ServerCredentials.Ip + ",1433" + "\\" + instance,
                NetworkLibrary     = "DBMSSOCN",
                UserID             = sqlServer.ServerCredentials.Username,
                Password           = sqlServer.ServerCredentials.Password,
                IntegratedSecurity = true,
                InitialCatalog     = sqlServer.SqlGeneratorParameters.DbName,
                ConnectTimeout     = 300
            };

            using (var connection = new SqlConnection(builder.ConnectionString))
            {
                try
                {
                    connection.Open();
                }
                catch (SqlException e)
                {
                    Logger.LogError("Unable to open SQL connection to instance " + instance + ".", sqlServer.ServerCredentials.Ip, e);
                    throw;
                }
                Logger.Log("Successfully connected to SQL server", Logger.LogLevel.Info, sqlServer.ServerCredentials.Ip);
                try
                {
                    CreateTable(connection);
                }
                catch (SqlException e)
                {
                    if (!e.Message.Contains("There is already an object named"))
                    {
                        Logger.LogError("Unable to create table.", sqlServer.ServerCredentials.Ip, e);
                        throw;
                    }
                }
                try
                {
                    _random = new Random();

                    Parallel.For(0, sqlServer.SqlGeneratorParameters.RowsToInsert, (action, state) =>
                    {
                        if (sqlServer.Cts.IsCancellationRequested)
                        {
                            sqlServer.ServerGeneratorStatus = Server.GeneratorStatus.Stopped;
                            state.Break();
                        }
                        var connection1 = new SqlConnection(builder.ConnectionString);
                        connection1.Open();
                        AddEntries(connection1, _random);
                        connection1.Close();
                        lock (Lock)
                        {
                            rowsAdded++;
                        }

                        UpdateProgress(sqlServer, rowsAdded, sqlServer.SqlGeneratorParameters.RowsToInsert);
                    });
                    sqlServer.Cts.Token.ThrowIfCancellationRequested();
                }
                catch (SqlException e)
                {
                    Logger.LogError("Unable to insert data to the SQL table.", sqlServer.ServerCredentials.Ip, e);
                    throw;
                }

                Logger.Log("Successfully completed SQL generation", Logger.LogLevel.Info, sqlServer.ServerCredentials.Ip);
            }
        }
Exemplo n.º 3
0
 private static void UpdateProgress(SqlServer sqlServer, int rowsAdded, int rowsToAdd)
 {
     sqlServer.Progress = rowsAdded*100/rowsToAdd;
 }
Exemplo n.º 4
0
        public static void StartGenerator(SqlServer sqlServer)
        {
            var rowsAdded = 0;
            var instance = GetSqlInstanceNameFromService(sqlServer.ServerCredentials.Ip, sqlServer.ServerCredentials.Username, sqlServer.ServerCredentials.Password);
            var builder = new SqlConnectionStringBuilder
            {
                DataSource = sqlServer.ServerCredentials.Ip +",1433" + "\\" + instance,
                NetworkLibrary = "DBMSSOCN",
                UserID = sqlServer.ServerCredentials.Username,
                Password = sqlServer.ServerCredentials.Password,
                IntegratedSecurity = true,
                InitialCatalog = sqlServer.SqlGeneratorParameters.DbName,
                ConnectTimeout = 300
            };

            using (var connection = new SqlConnection(builder.ConnectionString))
            {
                try
                {
                    connection.Open();
                }
                catch(SqlException e)
                {
                    Logger.LogError("Unable to open SQL connection to instance " + instance + ".", sqlServer.ServerCredentials.Ip,e);
                    throw;
                }
                Logger.Log("Successfully connected to SQL server",Logger.LogLevel.Info, sqlServer.ServerCredentials.Ip);
                try
                {
                    CreateTable(connection);
                }
                catch (SqlException e)
                {
                    if (!e.Message.Contains("There is already an object named"))
                    {
                        Logger.LogError("Unable to create table.", sqlServer.ServerCredentials.Ip, e);
                        throw;
                    }
                }
                try
                {
                    _random = new Random();

                    Parallel.For(0, sqlServer.SqlGeneratorParameters.RowsToInsert, (action, state) =>
                    {
                        if (sqlServer.Cts.IsCancellationRequested)
                        {
                            sqlServer.ServerGeneratorStatus = Server.GeneratorStatus.Stopped;
                            state.Break();
                        }
                        var connection1 = new SqlConnection(builder.ConnectionString);
                        connection1.Open();
                        AddEntries(connection1, _random);
                        connection1.Close();
                        lock (Lock)
                        {
                            rowsAdded++;
                        }

                        UpdateProgress(sqlServer,rowsAdded,sqlServer.SqlGeneratorParameters.RowsToInsert);
                    });
                    sqlServer.Cts.Token.ThrowIfCancellationRequested();
                }
                catch (SqlException e)
                {
                    Logger.LogError("Unable to insert data to the SQL table.", sqlServer.ServerCredentials.Ip,e);
                    throw;
                }

                Logger.Log("Successfully completed SQL generation", Logger.LogLevel.Info, sqlServer.ServerCredentials.Ip);
            }
        }
Exemplo n.º 5
0
 private static void AddNewSqlServerToListView(ListView listView, SqlServer server)
 {
     var lviNewServer = new ListViewItem(server.DisplayName);
     lviNewServer.SubItems.Add(server.ServerCredentials.Ip);
     lviNewServer.SubItems.Add(server.Repository);
     lviNewServer.SubItems.Add(server.ServerGeneratorStatus.ToString());
     lviNewServer.SubItems.Add(server.SqlGeneratorParameters?.DbName ?? "");
     lviNewServer.SubItems.Add(server.SqlGeneratorParameters?.RowsToInsert.ToString() ?? "");
     lviNewServer.SubItems.Add(server.Progress.ToString());
     listView.Items.Add(lviNewServer);
 }