From ecc886edc543052b2deff581ac0b19c7d5543d02 Mon Sep 17 00:00:00 2001 From: Fred Date: Tue, 5 Oct 2021 11:51:03 +0200 Subject: [PATCH] Clean code --- Scriptio/FormMain.cs | 39 +++++++++++---------------------------- 1 file changed, 11 insertions(+), 28 deletions(-) diff --git a/Scriptio/FormMain.cs b/Scriptio/FormMain.cs index 0165e8e..1a4c79c 100644 --- a/Scriptio/FormMain.cs +++ b/Scriptio/FormMain.cs @@ -1,7 +1,8 @@ /////////////////////////////////////////////////////////////////////////////////////////////// // Scriptio - Script SQL Server 2005 objects // Copyright (C) 2005 Bill Graziano and 2008 Riccardo Spagni - +// Copyright (C) MIT 2021 Freddy Juhel +// // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation; either version 2 of the License, or @@ -21,13 +22,6 @@ // TODO: (ie. optional extras:) // -// - NB: Check if this works with SQL 2008 (Katmai) -// -// - NB: Find a way of integrating with SQL 2005's context menu; maybe have Scriptio as a contex menu item on a -// database level, and then launch Scriptio with the authentication items already filled in, the database list -// populated with a single item (the database we've right clicked, obviously) and grayed out, and fire off the -// PopulateDatabases method. Also, see if we can't integrate it with SQL 2008 as well whilst we're about it. -// // - Build intelligence into the selection system, so that when you select an item in the DataGrid it checks if // all Stored Procs, for example, have been manually ticked in the DataGrid, and then reflects that in the // CheckedListBox by ticking the StoredProc's item. @@ -36,8 +30,6 @@ // UserDefinedTypes to the clbType items collection, it should add it as "User Defined Types". Beautify the // description in the DataGrid too so that they match. // -// - Add other encoding options (UTF-7, UTF-8, UTF-32, BigEndianUnicode), dependant on demand -// // - Make sure we've exposed ALL relevant ScriptingOptions properties to the user, you never know what oddity might // be required by a single person in a single environment @@ -114,7 +106,6 @@ private void PopulateDatabases(string serverName) try { SqlConnection connection = GetConnection("tempdb"); - //Server srv = new Server(new ServerConnection(connection)); Server server = new Server(serverName); // Check if we're using 2005 or higher @@ -142,8 +133,7 @@ private void PopulateDatabases(string serverName) } else { - MessageBox.Show("SMO Scripting is only available for SQL Server 2005 and higher", - "Incorrect Server Version", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("SMO Scripting is only available for SQL Server 2005 and higher", "Incorrect Server Version", MessageBoxButtons.OK, MessageBoxIcon.Error); dgAvailableObjects.Rows.Clear(); chkScriptAll.Checked = false; @@ -152,8 +142,7 @@ private void PopulateDatabases(string serverName) } catch (ConnectionFailureException) { - MessageBox.Show("Unable to connect to server", - "Invalid Server", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show("Unable to connect to server", "Invalid Server", MessageBoxButtons.OK, MessageBoxIcon.Error); } // RS: Reset the label text @@ -344,6 +333,7 @@ private string GetConnectionString(string databaseName) csb.UserID = txtUsername.Text; csb.Password = txtPassword.Text; } + csb.InitialCatalog = databaseName; csb.ApplicationName = "Scriptio"; return csb.ConnectionString; @@ -617,12 +607,14 @@ private void BtnScript_Click(object sender, EventArgs e) private void AddLines(StringCollection strings) { - foreach (string s in strings) + foreach (string oneString in strings) { - result.Append(Environment.NewLine + s); + result.Append(Environment.NewLine + oneString); - if (s.StartsWith("SET QUOTED_IDENTIFIER") || s.StartsWith("SET ANSI_NULLS")) + if (oneString.StartsWith("SET QUOTED_IDENTIFIER") || oneString.StartsWith("SET ANSI_NULLS")) + { result.Append(Environment.NewLine + "GO"); + } } } @@ -663,7 +655,7 @@ private void RdoOneFile_CheckedChanged(object sender, EventArgs e) private void RdoNoFiles_CheckedChanged(object sender, EventArgs e) { - txtSaveLocation.Text = string.Empty; + txtSaveLocation.Text = ""; chkNamingConventions.Enabled = false; } @@ -705,14 +697,6 @@ private void BtnSaveAs_Click(object sender, EventArgs e) Encoding encoding; encoding = chkGenerateASCII.Checked ? Encoding.ASCII : Encoding.Unicode; sw = new StreamWriter(saveFileDialog1.FileName, false, encoding); - //if (chkGenerateASCII.Checked) - //{ - // sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.ASCII); - //} - //else - //{ - // sw = new StreamWriter(saveFileDialog1.FileName, false, Encoding.Unicode); - //} foreach (string str in txtResult.Lines) { @@ -776,7 +760,6 @@ private void ClbSchema_SelectedIndexChanged(object sender, EventArgs e) private void SaveWindowValue() { - Properties.Settings.Default.WindowHeight = Height; Properties.Settings.Default.WindowWidth = Width; Properties.Settings.Default.WindowLeft = Left;