string ChangeTable(XElement actionElement) { List <XElement> addedColumns = actionElement.Elements("AddColumn").ToList(); List <XElement> removedColumns = actionElement.Elements("DropColumn").ToList(); List <XElement> changedColumns = actionElement.Elements("AlterColumn").ToList(); if (addedColumns.Count == 0 && removedColumns.Count == 0 && changedColumns.Count == 0) { return(String.Empty); } StringBuilder sb = new StringBuilder(); string rawTableName = actionElement.Attribute("name").Value; string tableName = this.provider.GetQualifiedTableName(rawTableName); string alterString = "ALTER TABLE " + tableName + ' '; foreach (XElement columnElement in addedColumns) { sb.Append(alterString); sb.Append(concreteGenerator.AddColumn()); sb.Append(' '); sb.Append(CreateColumn(columnElement, GetClassForTable(rawTableName, this.mappings), this.provider, false)); sb.Append(";\n"); } foreach (XElement columnElement in removedColumns) { sb.Append(alterString); sb.Append(concreteGenerator.RemoveColumn(provider.GetQuotedName(columnElement.Attribute("name").Value))); sb.Append(";\n"); } foreach (XElement columnElement in changedColumns) { sb.Append(alterString); sb.Append(concreteGenerator.AlterColumnType()); sb.Append(' '); sb.Append(CreateColumn(columnElement, GetClassForTable(rawTableName, this.mappings), this.provider, false)); sb.Append(";\n"); } return(sb.ToString()); }
public void AddPrimaryKeyColum(TableNode tn) { //try //{ AddPrimaryKeyWiz wiz = new AddPrimaryKeyWiz(); if (wiz.ShowDialog() != DialogResult.OK) { return; } foreach (ColumnNode cn in tn.ColumnNodes) { if (cn.IsPrimary) { cn.ChangePrimary(null, EventArgs.Empty); } } ISqlGenerator generator = (ISqlGenerator)NDOProviderFactory.Instance.Generators[this.databaseNode.Database.ConnectionType]; IProvider provider = NDOProviderFactory.Instance[generator.ProviderName]; generator.Provider = provider; string columnString = CreateColumn(generator, provider, wiz.ColumnName, wiz.ColumnType, wiz.IsAutoNumbered); string alter = "ALTER TABLE " + generator.AddColumn() + " " + columnString; IDbConnection conn = provider.NewConnection(databaseNode.Database.ConnectionString); IDbCommand cmd = provider.NewSqlCommand(conn); cmd.CommandText = alter; MessageBox.Show(alter); //cmd.ExecuteNonQuery(); //} //catch (Exception ex) //{ // MessageBox.Show(ex.Message, "Error"); //} // sb.Append(generator.NullExpression(false)); }