コード例 #1
0
        /// <summary>Called to run the command. Can throw on error.</summary>
        /// <param name="cancelToken">Is cancellation pending?</param>
        public void Run(CancellationTokenSource cancelToken)
        {
            if (dataToWrite.Rows.Count > 0)
            {
                if (!tables.TryGetValue(dataToWrite.TableName, out var table))
                {
                    table = new DatabaseTableDetails(connection, dataToWrite.TableName);
                    tables.Add(dataToWrite.TableName, table);
                }

                var query = new InsertQuery(dataToWrite);

                // Make sure the table has the correct columns.
                table.EnsureTableExistsAndHasRequiredColumns(dataToWrite);

                // Get a list of column names.
                var columnNames = dataToWrite.Columns.Cast <DataColumn>().Select(col => col.ColumnName);

                try
                {
                    connection.BeginTransaction();

                    // Write all rows.
                    foreach (DataRow row in dataToWrite.Rows)
                    {
                        query.ExecuteQuery(connection, columnNames, row.ItemArray);
                    }
                }
                finally
                {
                    connection.EndTransaction();
                    query.Close(connection);
                }
            }
        }
コード例 #2
0
ファイル: WriteTableCommand.cs プロジェクト: hol430/ApsimX
        /// <summary>Called to run the command. Can throw on error.</summary>
        /// <param name="cancelToken">Is cancellation pending?</param>
        public void Run(CancellationTokenSource cancelToken)
        {
            if (dataToWrite.Rows.Count > 0)
            {
                if (!tables.TryGetValue(dataToWrite.TableName, out var table))
                {
                    table = new DatabaseTableDetails(connection, dataToWrite.TableName);
                    tables.Add(dataToWrite.TableName, table);
                }

                var query = new InsertQuery(dataToWrite);

                // Make sure the table has the correct columns.
                table.EnsureTableExistsAndHasRequiredColumns(dataToWrite);

                // Get a list of column names.
                var columnNames = dataToWrite.Columns.Cast <DataColumn>().Select(col => col.ColumnName);

                try
                {
                    connection.BeginTransaction();

                    if (deleteExistingRows)
                    {
                        // fixme - this assumes that "Current" checkpoint ID is always 1.
                        // This should always be correct afaik, but it would be better to
                        // verify this at runtime.
                        bool tableHasCheckpointID = connection.GetColumns(dataToWrite.TableName).Any(c => c.Item1 == "CheckpointID");
                        connection.ExecuteNonQuery($"DELETE FROM [{dataToWrite.TableName}] {(tableHasCheckpointID ? "WHERE CheckpointID = 1" : "")}");
                    }
                    // Write all rows.
                    foreach (DataRow row in dataToWrite.Rows)
                    {
                        query.ExecuteQuery(connection, columnNames, row.ItemArray);
                    }
                }
                finally
                {
                    connection.EndTransaction();
                    query.Close(connection);
                }
            }
        }