示例#1
0
        private void Run(string[] args)
        {
            string service = "8001";
            bool ftEnable = false;

            // Check the correct number of arguments have been supplied
            if (args.Length > 2)
            {
                Console.WriteLine("usage: {0} [<data service> [FT]]", System.AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            // Check whether a service argument has been supplied
            if (args.Length >= 1)
            {
                service = args[0];
            }

            // Check wether an FT argument has been supplied
            if (args.Length >= 2)
            {
                ftEnable = args[1].Equals("FT");
            }

            // Build a connection string and connect to the database
            PolyConnectionStringBuilder builder = new PolyConnectionStringBuilder();

            builder.Service = service;

            // Set FT connection settings
            builder.FTEnable = ftEnable;
            builder.FTHeartbeatInterval = 1000;
            builder.FTHeartbeatTimeout = 1000;
            builder.FTReconnectionCount = 1000;
            builder.FTReconnectionInterval = 1000;
            builder.FTReconnectionTimeout = 1000;

            PolyConnection connection = new PolyConnection(builder.ConnectionString);

            // Register for state change events on the connection
            // This is used to detect loss of connection to the database
            connection.StateChange += OnStateChange;

            // Register for FT mode change events on the connection
            connection.FTModeChange += OnFTModeChange;

            Console.WriteLine("Connecting to {0}{1}...", service, ftEnable ? " Fault Tolerant" : "");

            try
            {
                connection.Open();
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to connect to database");
                return;
            }

            // Create and command and an active adapter to perform the active query
            Console.WriteLine("Connected, now launch active query...");

            PolyCommand command = new PolyCommand("select code,usdollar from currency", connection);

            PolyActiveDataAdapter adapter = new PolyActiveDataAdapter(command);

            // Primary key information must be added to the data table
            adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            DataSet dataSet = new DataSet();

            // Perform an initial fill of the data table
            adapter.Fill(dataSet, "currency");

            DataTable dataTable = dataSet.Tables["currency"];

            // Display the results of the initial fill
            WriteTable(dataTable);

            // Register for data change events
            // This indicates when a delta is available for the active query
            adapter.DataChange += OnDataChange;

            // Register for row changed and row deleted events on the data table
            // This is used to determine the changes made when applying a delta
            dataTable.RowChanged += OnRowChanged;
            dataTable.RowDeleted += OnRowDeleted;

            // Explicitly accept changes made to the data table by the delta
            adapter.AcceptChangesDuringFill = false;

            // Use the Upsert LoadOption to determine the changes made by the delta
            adapter.FillLoadOption = LoadOption.Upsert;

            while (WaitHandle.WaitOne())
            {
                // Stop when connection to the database is lost
                if (connection.State == ConnectionState.Closed)
                {
                    break;
                }

                // Apply the delta to the data table
                adapter.FillDelta(dataTable);

                Console.WriteLine("Delta complete - success.\n");

                // Accept the changes made to the data table by the delta
                dataTable.AcceptChanges();
            }

            connection.Close();
        }
示例#2
0
        private void PolyForm_Load(object sender, EventArgs args)
        {
            // Build a connection string and connect to the database
            PolyConnectionStringBuilder builder = new PolyConnectionStringBuilder();

            builder.Service = service;

            connection = new PolyConnection(builder.ConnectionString);

            DialogResult result = DialogResult.Retry;

            // Allow connection attempt to be retried if it fails
            while (result == DialogResult.Retry)
            {
                try
                {
                    connection.Open();
                    break;
                }
                catch (Exception exception)
                {
                    result = MessageBox.Show(GetExceptionMessage(exception), Text, MessageBoxButtons.RetryCancel);
                    if (result != DialogResult.Retry)
                    {
                        Close();
                        return;
                    }
                }
            }

            // Create an active data adapter to perform the active query (on the currency table)
            dataAdapter = new PolyActiveDataAdapter("select * from currency", connection);

            // Create a data table to hold the data
            dataTable = new DataTable();

            // Primary key information must be added to the data table
            dataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            // Register for data change events
            // This indicates when a delta is available for the active query
            dataAdapter.DataChange += OnDataChange;

            // Do not automatically accept changes made to the data table when updating
            dataAdapter.AcceptChangesDuringUpdate = false;

            // Start active query and obtain initial data
            dataAdapter.Fill(dataTable);

            // Provide the data table as the data source to the data grid
            dataGridView.DataSource = dataTable;

            // Sort data by currency code
            dataGridView.Sort(dataGridView.Columns["code"], ListSortDirection.Ascending);

            // Right-align column containing dollar value
            dataGridView.Columns["usdollar"].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;

            // Turn edit mode off
            SetEditMode(false);

            // Note that the connection must remain open for the active data adapter to function
        }
示例#3
0
        private void Run(string[] args)
        {
            string service = "8001";

            // Check the correct number of arguments have been supplied
            if (args.Length > 2)
            {
                Console.WriteLine("usage: {0} <max size of load_file> [<data service>]", System.AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            // Maximum file size allowed
            MaxFileSize = long.Parse(args[0]);

            // Check whether a service argument has been supplied
            if (args.Length >= 2)
            {
                service = args[1];
            }

            // Build a connection string and connect to the database
            PolyConnectionStringBuilder builder = new PolyConnectionStringBuilder();

            builder.Service = service;

            Connection = new PolyConnection(builder.ConnectionString);

            // Register for state change events on the connection
            // This is used to detect loss of connection to the database
            Connection.StateChange += OnStateChange;

            Console.WriteLine("Connecting to database at {0}...", service);

            try
            {
                Connection.Open();
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to connect to database");
                return;
            }

            // Create and command and an active adapter to perform the active query
            Console.WriteLine("Connected, now launch active query on journal control");
            Console.WriteLine("and monitor file_size for it reaching {0}", MaxFileSize);

            PolyCommand command = new PolyCommand("select id,file_size from journalcontrol", Connection);

            PolyActiveDataAdapter adapter = new PolyActiveDataAdapter(command);

            // Primary key information must be added to the data table
            adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            DataSet dataSet = new DataSet();

            // Perform an initial fill of the data table
            adapter.Fill(dataSet, "journalcontrol");

            DataTable dataTable = dataSet.Tables["journalcontrol"];

            // Display the results of the initial fill
            WriteTable(dataTable);

            // Register for data change events
            // This indicates when a delta is available for the active query
            adapter.DataChange += OnDataChange;

            while (WaitHandle.WaitOne())
            {
                // Stop when connection to the database is lost
                if (Connection.State == ConnectionState.Closed)
                {
                    break;
                }

                // Apply the delta to the data table
                adapter.FillDelta(dataTable);

                // Display the results of the delta
                WriteTable(dataTable);
            }

            Connection.Close();
        }
示例#4
0
        private void Run(string[] args)
        {
            string service = "8001";

            // Check the correct number of arguments have been supplied
            if (args.Length > 1)
            {
                Console.WriteLine("usage: {0} [<data service>]", System.AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            // Check whether a service argument has been supplied
            if (args.Length >= 1)
            {
                service = args[0];
            }

            // Build a connection string and connect to the database
            PolyConnectionStringBuilder builder = new PolyConnectionStringBuilder();

            builder.Service = service;

            PolyConnection connection = new PolyConnection(builder.ConnectionString);

            // Register for state change events on the connection
            // This is used to detect loss of connection to the database
            connection.StateChange += OnStateChange;

            Console.WriteLine("Connecting to {0}...", service);

            try
            {
                connection.Open();
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to connect to database");
                return;
            }

            // Create and command and an active adapter to perform the active query
            Console.WriteLine("Connected, now launch active query...");

            PolyCommand command = new PolyCommand("select code,usdollar,low_limit,high_limit from currency_limits", connection);

            PolyActiveDataAdapter adapter = new PolyActiveDataAdapter(command);

            // Primary key information must be added to the data table
            adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            DataSet dataSet = new DataSet();

            // Explicitly accept changes made to the data table by the delta
            adapter.AcceptChangesDuringFill = false;

            // Register for data change events
            // This indicates when a delta is available for the active query
            adapter.DataChange += OnDataChange;

            // Perform an initial fill of the data table
            adapter.Fill(dataSet, "currency_limits");

            DataTable dataTable = dataSet.Tables["currency_limits"];

            // Display the results of the initial fill
            WriteTable(dataTable);

            // Accept the changes made to the data table by the delta
            dataTable.AcceptChanges();

            // Use the Upsert LoadOption to determine the changes made by the delta
            adapter.FillLoadOption = LoadOption.Upsert;

            while (WaitHandle.WaitOne())
            {
                // Stop when connection to the database is lost
                if (connection.State == ConnectionState.Closed)
                {
                    break;
                }

                // Apply the delta to the data table
                adapter.FillDelta(dataTable);

                // Display the results of the delta
                WriteTable(dataTable);

                // Accept the changes made to the data table by the delta
                dataTable.AcceptChanges();
            }

            connection.Close();
        }
示例#5
0
        private void ChangeData(DataTable dataTable, PolyActiveDataAdapter adapter)
        {
            Console.WriteLine("Start update through active query");

            // Apply changes to the data table in a single transaction
            PolyTransaction transaction;

            try
            {
                transaction = adapter.SelectCommand.Connection.BeginTransaction();

                adapter.SelectCommand.Transaction = transaction;
            }
            catch (Exception e)
            {
                Console.WriteLine("BeginTransaction failure");
                WriteException(e);
                return;
            }

            foreach (DataRow dataRow in dataTable.Rows)
            {
                double usdollar_starting = (double)dataRow["usdollar_starting"];
                string code = (string)dataRow["code"];
                double usdollar = (double)dataRow["usdollar"];
                double change = (double)dataRow["change"];

                // Work out the change we want to make
                if (change == 0)
                {
                    usdollar_starting = usdollar;
                    change = usdollar * 0.01;

                    dataRow["usdollar_starting"] = usdollar_starting;
                }

                if (change > 0)
                {
                    if (usdollar + change > usdollar_starting * 1.1)
                    {
                        Console.WriteLine("Upper limit crossed");
                        change = -change;
                    }
                }
                else
                {
                    if (usdollar + change <= usdollar_starting * 0.9)
                    {
                        Console.WriteLine("Lower limit crossed");
                        change = -change;
                    }
                }

                usdollar += change;

                Console.WriteLine("Change usdollar field of {0} by {1:F6} to {2:F6}", code, change, usdollar);

                dataRow["usdollar"] = usdollar;
                dataRow["change"] = change;
            }

            // If the table is not empty, commit the changes we have just set up
            if (dataTable.Rows.Count > 0)
            {
                try
                {
                    adapter.Update(dataTable);

                    transaction.Commit();
                    Console.WriteLine("Transaction complete");
                }
                catch (Exception e)
                {
                    Console.WriteLine("Commit failure");
                    WriteException(e);
                }
            }
        }
示例#6
0
        private void Run(string[] args)
        {
            string service = "8001";
            bool ftEnable = false;

            // Check the correct number of arguments have been supplied
            if (args.Length > 2)
            {
                Console.WriteLine("usage: {0} [<data service> [FT]]", System.AppDomain.CurrentDomain.FriendlyName);
                return;
            }

            // Check whether a service argument has been supplied
            if (args.Length >= 1)
            {
                service = args[0];
            }

            // Check wether an FT argument has been supplied
            if (args.Length >= 2)
            {
                ftEnable = args[1].Equals("FT");
            }

            // Build a connection string and connect to the database
            PolyConnectionStringBuilder builder = new PolyConnectionStringBuilder();

            builder.Service = service;

            // Set FT connection settings
            builder.FTEnable = ftEnable;
            builder.FTHeartbeatInterval = 1000;
            builder.FTHeartbeatTimeout = 1000;
            builder.FTReconnectionCount = 1000;
            builder.FTReconnectionInterval = 1000;
            builder.FTReconnectionTimeout = 1000;

            PolyConnection connection = new PolyConnection(builder.ConnectionString);

            // Register for state change events on the connection
            // This is used to detect loss of connection to the database
            connection.StateChange += OnStateChange;

            // Register for FT mode change events on the connection
            connection.FTModeChange += OnFTModeChange;

            Console.WriteLine("Connecting to {0}{1}...", service, ftEnable ? " Fault Tolerant" : "");

            try
            {
                connection.Open();
            }
            catch (Exception)
            {
                Console.WriteLine("Failed to connect to database");
                return;
            }

            Console.WriteLine("Connection made successfully.");

            // Create an active data adapter to perform the active query
            PolyActiveDataAdapter adapter = new PolyActiveDataAdapter("select code,usdollar from currency", connection);

            // Primary key information must be added to the data table
            adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            // Create a data table to hold the result
            DataSet dataSet = new DataSet();

            DataTable dataTable = dataSet.Tables.Add("currency");

            // Add a column to record the starting usdollar value
            DataColumn dataColumn = dataTable.Columns.Add("usdollar_starting", typeof(double));

            dataColumn.DefaultValue = 0.0;

            // Add a column to record the last change made to the currency
            dataColumn = dataTable.Columns.Add("change", typeof(double));

            dataColumn.DefaultValue = 0.0;

            // Perform an initial fill of the data table
            adapter.Fill(dataSet, "currency");

            // Change the results of the initial fill
            ChangeData(dataTable, adapter);

            // Register for data change events
            // This indicates when a delta is available for the active query
            adapter.DataChange += OnDataChange;

            while (true)
            {
                if (WaitHandle.WaitOne(2000))
                {
                    // Stop when connection to the database is lost
                    if (connection.State == ConnectionState.Closed)
                    {
                        break;
                    }

                    // Apply the delta to the data table
                    adapter.FillDelta(dataTable);
                }
                else
                {
                    ChangeData(dataTable, adapter);
                }
            }

            connection.Close();
        }