public static void InstallDatabase(string connectionString)
        {
            string message;
            SqlConnectionStringBuilder csB = new SqlConnectionStringBuilder(connectionString);

            if (SqlUtilities.DatabaseExist(csB))
            {
                message = string.Format("--- Deleting Previous Database [{0}]...", csB.InitialCatalog);
                MyLogger.ShowMessage(message);
                SqlUtilities.DeleteDbIfExists(csB, 1000, MyCallbacks.MyDatabaseCallback);
            }

            message = string.Format("--- Creating New Database [{0}]...", csB.InitialCatalog);
            MyLogger.ShowMessage(message);

            SqlUtilities.CreateDatabase(csB, 1000, MyCallbacks.MyDatabaseCallback);

            CreateSqlTables(connectionString);
        }
Ejemplo n.º 2
0
        public bool IsConnectionStringValid(out string connectionErrorMessage, List <string> databaseLocations)
        {
            try
            {
                if (Mode == DbConfigurationMode.Create)
                {
                    if (sqlServerConfiguration.Visible)
                    {
                        SqlConnectionStringBuilder csB;


                        csB = new SqlConnectionStringBuilder(sqlServerConfiguration.ConnectionString);

                        if (string.IsNullOrEmpty(csB.InitialCatalog))
                        {
                            connectionErrorMessage = "Database name is empty";

                            return(false);
                        }
                        else
                        {
                            string dbName;
                            string fop;


                            dbName = csB.InitialCatalog;
                            fop    = csB.FailoverPartner;

                            csB.InitialCatalog  = string.Empty;
                            csB.FailoverPartner = string.Empty;

                            if (SqlUtilities.TestSQLConnectionString(csB.ConnectionString, out connectionErrorMessage))
                            {
                                csB.InitialCatalog  = dbName;
                                csB.FailoverPartner = fop;

                                if (IsDatabaseLocationAlreadyUsed(databaseLocations, ConnectionString, csB.InitialCatalog, out connectionErrorMessage))
                                {
                                    return(false);
                                }

                                else if (SqlUtilities.DatabaseExist(csB))
                                {
                                    DialogResult deleteExistent;


                                    deleteExistent = Messager.ShowQuestion(this,
                                                                           "The database you selected already exists.\n\nDo you want to delete the existing database and create a new one?\n\n" +
                                                                           "Click 'Yes' to delete the existing database or click 'No' to change the database name.",
                                                                           MessageBoxButtons.YesNo);

                                    if (deleteExistent == DialogResult.No)
                                    {
                                        connectionErrorMessage = string.Empty;//string.Format ( "Database {0} already exists. Please select a different database name.", dbName ) ;

                                        sqlServerConfiguration.SetComponentFocus(SqlServerConfiguration.ConnectionStringCommponents.Database);

                                        return(false);
                                    }
                                    else
                                    {
                                        connectionErrorMessage = string.Empty;

                                        return(true);
                                    }
                                }
                                else
                                {
                                    connectionErrorMessage = string.Empty;

                                    return(true);
                                }
                            }
                            else
                            {
                                sqlServerConfiguration.SetComponentFocus(SqlServerConfiguration.ConnectionStringCommponents.Server);

                                return(false);
                            }
                        }
                    }
                    else
                    {
                        connectionErrorMessage = string.Empty;

                        if (String.IsNullOrEmpty(sqlCeConfiguration.ConnectionString))
                        {
                            connectionErrorMessage = "Database name is empty";

                            return(false);
                        }
                        else if (IsDatabaseLocationAlreadyUsed(databaseLocations, ConnectionString, sqlCeConfiguration.DatabaseLocation, out connectionErrorMessage))
                        {
                            return(false);
                        }
                        else if (File.Exists(sqlCeConfiguration.DatabaseLocation))
                        {
                            DialogResult deleteExistent;


                            deleteExistent = Messager.ShowQuestion(this,
                                                                   "The database you selected already exists.\n\nDo you want to delete the existing database and create a new one?\n\n" +
                                                                   "Click 'Yes' to delete the existing database or click 'No' to change the database name.",
                                                                   MessageBoxButtons.YesNo);

                            if (deleteExistent == DialogResult.No)
                            {
                                connectionErrorMessage = string.Empty; //string.Format ( "Database {0} already exists. Please select a different database name.", sqlCeConfiguration.DatabaseLocation ) ;

                                return(false);
                            }
                            else
                            {
                                return(true);
                            }
                        }
                        else
                        {
                            return(true);
                        }
                    }
                }
                else
                {
                    if (sqlServerConfiguration.Visible)
                    {
                        SqlConnectionStringBuilder csB = new SqlConnectionStringBuilder(sqlServerConfiguration.ConnectionString);

                        if (string.IsNullOrEmpty(csB.InitialCatalog))
                        {
                            connectionErrorMessage = "Database name is empty";

                            return(false);
                        }

                        csB.Pooling = false;

                        if (IsDatabaseLocationAlreadyUsed(databaseLocations, ConnectionString, csB.InitialCatalog, out connectionErrorMessage))
                        {
                            return(false);
                        }

                        return(SqlUtilities.TestSQLConnectionString(csB.ConnectionString, out connectionErrorMessage));
                    }
                    else
                    {
                        return(IsConnectionStringValidSqlCe(databaseLocations, out connectionErrorMessage));
                    }
                }
            }
            catch (Exception exception)
            {
                System.Diagnostics.Debug.Assert(false, exception.Message);

                throw;
            }
        }