protected void YesButton_Click(object sender, System.EventArgs e) { SqlServer server = SqlServer.CurrentServer; server.Connect(); SqlDatabase database = SqlDatabase.CurrentDatabase(server); SqlTable table = database.Tables[Request["table"]]; if (table == null) { server.Disconnect(); // Table doesn't exist - break out and go to error page Response.Redirect(String.Format("error.aspx?error={0}", 1002)); return; } // Delete the table table.Remove(); server.Disconnect(); // Redirect to info page Response.Redirect("tables.aspx?database=" + Server.UrlEncode(Request["database"])); }
protected void CreateNewTableButton_Click(object sender, System.EventArgs e) { if (TableNameTextBox.Text.Length == 0) { ErrorCreatingLabel.Visible = true; ErrorCreatingLabel.Text = "The new table name cannot be blank"; return; } SqlServer server = SqlServer.CurrentServer; try { server.Connect(); } catch (System.Exception ex) { //Response.Redirect("Error.aspx?errorPassCode=" + 2002); Response.Redirect(String.Format("error.aspx?errormsg={0}&stacktrace={1}", Server.UrlEncode(ex.Message), Server.UrlEncode(ex.StackTrace))); } SqlDatabase database = SqlDatabase.CurrentDatabase(server); ErrorCreatingLabel.Visible = false; SqlTable table = server.Databases[database.Name].Tables[TableNameTextBox.Text]; // Ensure that the table doesn't exist yet if (table == null) { // Now we have to do a quick check and see if it's a valid name for a table // The only reliable way to do this is to try to create the table and see what happens // In order to find out whether the table name is valid, we create a temporary dummy table // and see what happens. SqlTable dummyTable = null; try { dummyTable = database.Tables.Add(TableNameTextBox.Text, new SqlColumnInformation[] { new SqlColumnInformation("Column1") }); } catch (Exception ex) { // Disconnect and show error if (dummyTable != null) { dummyTable.Remove(); } server.Disconnect(); ErrorCreatingLabel.Visible = true; ErrorCreatingLabel.Text = "There was an error creating the table:<br>" + Server.HtmlEncode(ex.Message).Replace("\n", "<br>"); return; } // Delete the dummy table dummyTable.Remove(); server.Disconnect(); Response.Redirect(String.Format("editcolumn.aspx?database={0}&table={1}", Server.UrlEncode(database.Name), Server.UrlEncode(TableNameTextBox.Text))); } else { server.Disconnect(); ErrorCreatingLabel.Visible = true; ErrorCreatingLabel.Text = "A table with this name already exists."; } }
protected void UpdateButton_Click(object sender, System.EventArgs e) { if (!IsValid) { return; } SqlServer server = SqlServer.CurrentServer; server.Connect(); SqlDatabase database = SqlDatabase.CurrentDatabase(server); // Parse user input and stick it into ColumnInfo SqlColumnInformation columnInfo = new SqlColumnInformation(); columnInfo.Key = PrimaryKeyCheckbox.Checked; columnInfo.Name = ColumnNameTextbox.Text; columnInfo.DataType = DataTypeDropdownlist.SelectedItem.Text; try { columnInfo.Size = Convert.ToInt32(LengthTextbox.Text); } catch { // Show error and quit ErrorUpdatingColumnLabel.Visible = true; ErrorUpdatingColumnLabel.Text = "Invalid input: Size must be an integer"; return; } columnInfo.Nulls = AllowNullCheckbox.Checked; columnInfo.DefaultValue = DefaultValueTextbox.Text; try { columnInfo.Precision = Convert.ToInt32(PrecisionTextbox.Text); } catch { // Show error and quit ErrorUpdatingColumnLabel.Visible = true; ErrorUpdatingColumnLabel.Text = "Invalid input: Precision must be an integer"; return; } try { columnInfo.Scale = Convert.ToInt32(ScaleTextbox.Text); } catch { // Show error and quit ErrorUpdatingColumnLabel.Visible = true; ErrorUpdatingColumnLabel.Text = "Invalid input: Scale must be an integer"; return; } columnInfo.Identity = IdentityCheckBox.Checked; try { columnInfo.IdentitySeed = Convert.ToInt32(IdentitySeedTextbox.Text); } catch { // Show error and quit ErrorUpdatingColumnLabel.Visible = true; ErrorUpdatingColumnLabel.Text = "Invalid input: Identity seed must be an integer"; return; } try { columnInfo.IdentityIncrement = Convert.ToInt32(IdentityIncrementTextbox.Text); } catch { // Show error and quit ErrorUpdatingColumnLabel.Visible = true; ErrorUpdatingColumnLabel.Text = "Invalid input: Identity increment must be an integer"; return; } columnInfo.IsRowGuid = IsRowGuidCheckBox.Checked; SqlTable table = database.Tables[Request["table"]]; // First check if the table exists or not // If it doesn't exist, that means we are adding the first column of a new table // If it does exist, then either we are adding a new column to an existing table // or we are editing an existing column in an existing table if (table == null) { // Table does not exist - create a new table and add the new column try { SqlColumnInformation[] columnInfos = new SqlColumnInformation[1] { columnInfo }; table = database.Tables.Add(Request["table"], columnInfos); } catch (Exception ex) { // If the table was somehow created, get rid of it table = database.Tables[Request["table"]]; if (table != null) { table.Remove(); } // Show error and quit ErrorUpdatingColumnLabel.Visible = true; ErrorUpdatingColumnLabel.Text = "The following error occured while trying to apply the changes.<br>" + Server.HtmlEncode(ex.Message).Replace("\n", "<br>"); server.Disconnect(); return; } } else { // Table does exist, do further check // If original name is blank that means it is a new column string originalColumnName = Request["column"]; if (originalColumnName == null || originalColumnName.Length == 0) { try { table.Columns.Add(columnInfo); } catch (Exception ex) { // Show error and quit ErrorUpdatingColumnLabel.Visible = true; ErrorUpdatingColumnLabel.Text = "The following error occured while trying to apply the changes:<br>" + Server.HtmlEncode(ex.Message).Replace("\n", "<br>"); server.Disconnect(); return; } } else { // If we get here that means we are editing an existing column // Simply set the column info - internally the table gets recreated try { table.Columns[originalColumnName].ColumnInformation = columnInfo; } catch (Exception ex) { // Show error and quit ErrorUpdatingColumnLabel.Visible = true; ErrorUpdatingColumnLabel.Text = "The following error occured while trying to apply the changes.<br>" + Server.HtmlEncode(ex.Message).Replace("\n", "<br>"); server.Disconnect(); return; } } } server.Disconnect(); // If we get here then that means a column was successfully added/edited Response.Redirect(String.Format("columns.aspx?database={0}&table={1}", Server.UrlEncode(Request["database"]), Server.UrlEncode(Request["table"]))); }