コード例 #1
0
        /// <summary>Alter an existing table ensuring all columns exist.</summary>
        /// <param name="table">The table definition to write to the database.</param>
        private void AlterTable(DataTable table)
        {
            bool haveBegunTransaction = false;

            foreach (DataColumn column in table.Columns)
            {
                if (!columnNamesInDb.Contains(column.ColumnName))
                {
                    if (!haveBegunTransaction)
                    {
                        haveBegunTransaction = true;
                        connection.BeginTransaction();
                    }

                    // Column is missing from database file - write it.
                    connection.AddColumn(Name, column.ColumnName, connection.GetDBDataTypeName(column.DataType));
                    columnNamesInDb.Add(column.ColumnName);
                }
            }

            // End the transaction that we started above.
            if (haveBegunTransaction)
            {
                connection.EndTransaction();
            }
        }
コード例 #2
0
ファイル: Table.cs プロジェクト: ianzhangtianyi/ApsimX
        /// <summary>Alter an existing table ensuring all columns exist.</summary>
        /// <param name="connection">The database connection to write to</param>
        private void AlterTable(IDatabaseConnection connection)
        {
            List <string> existingColumns = connection.GetTableColumns(Name);

            lock (lockObject)
            {
                foreach (Column col in Columns)
                {
                    if (!existingColumns.Contains(col.Name))
                    {
                        string dataTypeString;
                        if (col.DatabaseDataType == null)
                        {
                            dataTypeString = "integer";
                        }
                        else
                        {
                            dataTypeString = col.DatabaseDataType;
                        }

                        connection.AddColumn(Name, col.Name, dataTypeString);
                    }
                }
            }
        }