/// <summary> /// This method is used to test a SQL database connection. /// </summary> public static bool TestDatabaseConnection(SQLDatabaseConnector sqlDatabaseConnector) { // initial value bool connectionAvailable = false; try { // verify the sqlDatabaseConnector exists and the ConnectionString is set if ((sqlDatabaseConnector != null) && (!String.IsNullOrEmpty(sqlDatabaseConnector.ConnectionString))) { // Open the connection sqlDatabaseConnector.Open(); // Test the connection connectionAvailable = sqlDatabaseConnector.Connected; // Close the connection sqlDatabaseConnector.Close(); } } catch (Exception error) { // for debugging only string err = error.ToString(); } // return value return(connectionAvailable); }
/// <summary> /// This method loads the Database Schema for the database given. /// </summary> private void LoadDatabaseSchema(SQLDatabaseConnector connector, ref Database database) { try { // if the database exists if ((connector != null) && (database != null)) { // Open the connection connector.Open(); // if the connector is open if (connector.Connected) { // Load the database schema connector.LoadDatabaseSchema(database); } // Close the connection connector.Close(); // Open the connection connector.Open(); } } catch (Exception error) { // for debugging only string err = error.ToString(); } }
public bool IsString(DataJuggler.Net.DataTable table, string fieldName) { // initial value bool isString = false; // Create Generic row DataJuggler.Net.DataRow row = new DataRow(); // Get the Index Of Fieldname int Index = SQLDatabaseConnector.FindFieldIndex(table.Fields, fieldName); // If Index Was Found if (Index >= 0) { // if this field is a string if (table.Fields[Index].DataType == DataManager.DataTypeEnum.String) { // Use Quotes For Strings isString = true; } } // return value return(isString); }
/// <summary> /// This event is fired when the Test Database Connection button is clieccked. /// </summary> private void TestDatabaseConnectionButton_Click(object sender, EventArgs e) { try { // set the connectionTest bool connectionTest = false; // Set the connectionString string connectionString = this.ConnectionStringTextBox.Text; // test for a connectionString bool hasConnectionString = (!String.IsNullOrEmpty(connectionString)); // if a connection string has been established if (hasConnectionString) { // set the dataConnector SQLDatabaseConnector dataConnector = new SQLDatabaseConnector(); // set the connection string dataConnector.ConnectionString = connectionString; // Test the connection connectionTest = SQLDatabaseTester.TestDatabaseConnection(dataConnector); // if the connection test passed if (connectionTest) { // Show success icon PassedImage.Visible = true; } else { // Show a success message FailedImage.Visible = false; } // Start the timer to hide the images in 3 seconds Timer.Start(); } else { // Show a warning message MessageBox.Show("You must build or enter a connection string.", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception error) { // for debugging only string err = error.ToString(); // Show a success message MessageBox.Show("A connection to the database count not be estalished.", "Connection Test Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// event is fired when the 'GetSchemaHashButton' is clicked. /// </summary> private void GetSchemaHashButton_Click(object sender, EventArgs e) { // create a database Database database = new Database(); // paste in your connectionstring database.ConnectionString = ConnectionStringControl.Text; try { // if the connecionstring exists if (TextHelper.Exists(database.ConnectionString)) { // Create a new instance of a 'SQLDatabaseConnector' object. SQLDatabaseConnector connector = new SQLDatabaseConnector(); // set the connectionstring on the connector connector.ConnectionString = database.ConnectionString; // open the connection connector.Open(); // get the schemaHash string schemaHash = connector.GetDatabaseSchemaHash(database); // close the connection connector.Close(); // Display the result this.SchemaHashControl.Text = schemaHash; } else { // Show the user a message MessageBoxHelper.ShowMessage("You must paste in a connection string to continue.", "Connectionstring Required"); } } catch (Exception error) { // Show the user a message MessageBoxHelper.ShowMessage("An error occurred: " + error.ToString(), "Error"); } }
/// <summary> /// This method returns the Xml File And SQL Database /// </summary> public bool ValidateXmlFileAndSQLDatabase() { // initial value bool isValid = false; // local bool targetDatabaseConnectionPassed = false; bool sourceXmlFileIsValid = false; // if the SourceXmlFilePath exists and the TargetDatabaseConnectionString exists if ((this.HasSourceXmlFilePath) && (this.HasTargetDatabaseConnectionString)) { // Create The TargetDatabaseConnector this.targetDatabaseConnector = new SQLDatabaseConnector(); // Set the ConnectionString this.TargetDatabaseConnector.ConnectionString = this.TargetDatabaseConnectionString; // test the TargetDatabaseConnector targetDatabaseConnectionPassed = SQLDatabaseTester.TestDatabaseConnection(this.TargetDatabaseConnector); // set the sourceXmlFileIsValid sourceXmlFileIsValid = ValidateXmlFile(this.SourceXmlFilePath, true); // For now set this to valid, the Xml isValid = ((targetDatabaseConnectionPassed) && (sourceXmlFileIsValid)); // if the value for isValid is false if (!isValid) { // if the TargetDatabaseConnectionPassed is false and the sourceXmlFile is not valid if ((!targetDatabaseConnectionPassed) && (!sourceXmlFileIsValid)) { // The Source Xml File is not valid this.InvalidReason = "The 'Source Xml File' is not valid and the 'Target Database Connection' failed to connect"; } else if (!targetDatabaseConnectionPassed) { // The Source Xml File is not valid this.InvalidReason = "The 'Target Database Connection' failed to connect"; } else { // The Source Xml File is not valid this.InvalidReason = "The 'Source Xml File' is not valid"; } } } else if ((!this.HasTargetDatabaseConnectionString) && (!sourceXmlFileIsValid)) { // If the InvalidReason this.InvalidReason = "The 'Target Connection String' and the 'Source Xml File' must both be set"; } else if (!this.HasTargetDatabaseConnectionString) { // If the InvalidReason this.InvalidReason = "The 'Target Connection String' must be set."; } else if (!this.HasSourceXmlFilePath) { // If the InvalidReason this.InvalidReason = "The 'Source Xml File' must be set"; } // return value return(isValid); }
/// <summary> /// This event is fired when the Test Database Connection button is clieccked. /// </summary> private void TestDatabaseConnectionButton_Click(object sender, EventArgs e) { try { // set the connectionTest bool connectionTest = false; // Set the connectionString string connectionString = this.ConnectionstringControl.Text; // test for a connectionString bool hasConnectionString = (!String.IsNullOrEmpty(connectionString)); // if a connection string has been established if (hasConnectionString) { // set the dataConnector SQLDatabaseConnector dataConnector = new SQLDatabaseConnector(); // set the connection string dataConnector.ConnectionString = connectionString; // Test the connection connectionTest = SQLDatabaseTester.TestDatabaseConnection(dataConnector); // if the connection test passed if (connectionTest) { // Change the text to passed this.StatusLabel.Text = "Database Connection Test Passed."; // Show Success this.StatusImage.BackgroundImage = Properties.Resources.Success; // Set the text to the clipboard Clipboard.SetText(connectionString); // Show the CopiedImage and start the CopiedTimer ShowCopiedImage(); } else { // Show a success message this.StatusLabel.Text = "Database Connection Failed."; // Show Failure this.StatusImage.BackgroundImage = Properties.Resources.Failure; } // Show the StatusLabel and Image this.StatusLabel.Visible = true; this.StatusImage.Visible = true; } else { // Show a warning message MessageBox.Show("You must build or enter a connection string.", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Warning); } } catch (Exception error) { // for debugging only string err = error.ToString(); // Show a success message MessageBox.Show("A connection to the database count not be estalished.", "Connection Test Failed", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
/// <summary> /// Converts a ObjectLibrary.Database /// SQLServer database to a DataJuggler.Net Database /// and reads the schema. /// </summary> /// <param name="databases"></param> /// <returns></returns> private Database ConvertSQLDatabase(DTNDatabase db, List <Enumeration> enumerations) { // Create sqlConnector SQLDatabaseConnector sqlConnector = new SQLDatabaseConnector(); // if the tables are not loaded for the db if ((NullHelper.Exists(db)) && (!ListHelper.HasOneOrMoreItems(db.Tables))) { // Create a new instance of a 'Gateway' object. Gateway gateway = new Gateway(); // load the tables db.Tables = gateway.LoadDTNTablesByProjectId(this.CurrentProject.ProjectId); } // if the DataManager does not exist if (NullHelper.IsNull(DataManager)) { if (NullHelper.Exists(CurrentProject)) { // Don't know why this is still here, VB.Net was abandoned in 2006 or 2007 DataManager = new DataManager(CurrentProject.ProjectFolder, currentProject.ProjectName, DataManager.ClassOutputLanguage.CSharp); } else { // exit return(null); } } // Create Database DataJuggler.Net.Database database = new Database(this.DataManager); // Set Database Properties database.ClassFileName = this.DataManager.ClassFileName; database.ClassName = db.DatabaseName + ".cs"; database.ConnectionString = db.ConnectionString; database.Name = db.DatabaseName; database.ParentDataManager = this.DataManager; database.Password = db.DBPassword; database.Serializable = true; database.StoredProcedures = new List <StoredProcedure>(); // set connection string sqlConnector.DatabaseConnection.ConnectionString = database.ConnectionString; // open database sqlConnector.DatabaseConnection.Open(); // read database schema database = sqlConnector.LoadDatabaseSchema(database); // close this database sqlConnector.DatabaseConnection.Close(); // if there are one or more tables if (ListHelper.HasOneOrMoreItems(database.Tables)) { // iterate the collection of tables foreach (DataTable table in database.Tables) { // if a DotNet5 project and EnableBlazorFeatures is true and BindingCallBack option is set to CreateBinding if ((currentProject.TargetFramework != TargetFrameworkEnum.NetFramework) && (currentProject.EnableBlazorFeatures) && (currentProject.BindingCallbackOption == BindingCallbackOptionEnum.Create_Binding)) { // Create the BindingCall back needs to be set to true to code generate the Callback. table.CreateBindingCallback = true; } // if there are one or more fields if (ListHelper.HasOneOrMoreItems(table.Fields, enumerations)) { // iterate the fields foreach (DataField field in table.Fields) { // now iterate te enumerations foreach (Enumeration enumeration in enumerations) { // if this field is designated as an enumeration if (TextHelper.IsEqual(field.FieldName, enumeration.FieldName)) { // set to true field.IsEnumeration = true; // Set this as enumeration field.DataType = DataManager.DataTypeEnum.Enumeration; // Set the EnumerationName field.EnumDataTypeName = enumeration.EnumerationName; } } } } } } // return value return(database); }
/// <summary> /// This method returns the Two SQL Databases /// </summary> public bool ValidateTwoSQLDatabases() { // initial value bool isValid = false; // if the SourceDatabaseConnectionString and the TargetDatabaseConnectionString if ((this.HasSourceDatabaseConnectionString) && (this.HasTargetDatabaseConnectionString)) { // Create a new instance of a 'SQLDatabaseConnector' object. this.SourceDatabaseConnector = new SQLDatabaseConnector(); this.TargetDatabaseConnector = new SQLDatabaseConnector(); // Set the Source & Target ConnectionString this.SourceDatabaseConnector.ConnectionString = this.SourceDatabaseConnectionString; this.TargetDatabaseConnector.ConnectionString = this.TargetDatabaseConnectionString; // test the sourceConnectionTested bool sourceConnectionTested = SQLDatabaseTester.TestDatabaseConnection(this.SourceDatabaseConnector); bool targetConnectionTested = SQLDatabaseTester.TestDatabaseConnection(this.TargetDatabaseConnector); // if both connections tested if ((sourceConnectionTested) && (targetConnectionTested)) { // this is a valid connection isValid = true; } else if ((!sourceConnectionTested) && (targetConnectionTested)) { // Set the InvalidReason this.InvalidReason = "The source and target connections failed to connect."; } else if (!sourceConnectionTested) { // Set the InvalidReason this.InvalidReason = "The source connection failed to connect."; } else { // The Target Connection Failed this.InvalidReason = "The target connection failed to connect."; } } else if ((!this.HasSourceDatabaseConnectionString) && (!this.HasTargetDatabaseConnectionString)) { // Set the InvalidReason this.InvalidReason = "The 'Source Connection String' and the 'Target Connection String' must both be set"; } else if (!this.HasSourceDatabaseConnectionString) { // Set the InvalidReason this.InvalidReason = "The 'Source Connection String' must be set"; } else if (!this.HasTargetDatabaseConnectionString) { // Set the InvalidReason this.InvalidReason = "The 'Target Connection String' must be set"; } // return value return isValid; }