private void btnGerarProc_Click(object sender, EventArgs e) { txtInsertUpdate.Text = String.Empty; txtUpdate.Text = String.Empty; txtDelete.Text = String.Empty; txtPesquisa.Text = String.Empty; txtDuplicar.Text = String.Empty; txtViews.Text = String.Empty; objbo.setTabela(lbTabelas.ListBox.Text); txtInsertUpdate.Text = objbo.GerarInsert_Update_SP(); txtUpdate.Text = objbo.GerarUpdate_SP(); txtDelete.Text = objbo.GerarDelete_SP(); txtPesquisa.Text = objbo.GerarSelect_SP(); txtDuplicar.Text = objbo.GerarDuplicar_SP(); txtViews.Text = objbo.GetView(lbTabelas.SelectedValue.ToString()); txtConstraints.Text = ""; constraintsModel objConstraints = null; foreach (var item in clBoxColunas.CheckedItems) { objConstraints = new constraintsModel(); objConstraints.sColumnName = item.ToString(); objConstraints.sTabela = lbTabelas.SelectedValue.ToString(); objConstraints.sConstrName = "constr" + "_" + lbTabelas.SelectedValue.ToString() + "_"+item.ToString(); txtConstraints.Text += objConstbo.MontaScript(objConstr: objConstraints)+Environment.NewLine; } }
public string MontaScript(constraintsModel objConstr) { StringBuilder sb = new StringBuilder(); sb.Append("IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE i "); sb.Append(Environment.NewLine); sb.Append("where i.CONSTRAINT_NAME = '"+objConstr.sConstrName+"' and i.TABLE_NAME = '"+objConstr.sTabela+"')"); sb.Append(Environment.NewLine); sb.Append("begin"); sb.Append(Environment.NewLine); sb.Append("ALTER TABLE [dbo].["+objConstr.sTabela+"]"); sb.Append(Environment.NewLine); sb.Append("DROP CONSTRAINT ["+objConstr.sConstrName+"]"); sb.Append(Environment.NewLine); sb.Append("end"); sb.Append(Environment.NewLine); sb.Append("ALTER TABLE [dbo].["+objConstr.sTabela+"] ADD CONSTRAINT ["+objConstr.sConstrName+"]"+ " UNIQUE NONCLUSTERED"); sb.Append(Environment.NewLine); sb.Append("("); sb.Append(Environment.NewLine); sb.Append("["+objConstr.sColumnName+"] ASC"); sb.Append(Environment.NewLine); sb.Append(");"); return sb.ToString(); }