예제 #1
0
        static void Main(string[] args)
        {
            if (args.Length != 2)
            {
                Console.WriteLine("Usage: CreateAndSetDatabase <SQLServer> <DataBaseName>");
                return;
            }
            string sqlMasterConnectionString = "server=" + args[0] + ";database=master;integrated security=SSPI;";
            string dbName = args[1];

            SqlConnection sc = new SqlConnection(sqlMasterConnectionString);

            sc.Open();
            SqlCommand com = new SqlCommand("CREATE DATABASE " + dbName, sc);

            Console.Write("Preparing database ...");
            try
            {
                com.ExecuteNonQuery();
                Console.Write(" database [" + args[0] + "." + dbName + "] created.");
            }
            catch (SqlException se) {
                if (!se.Message.Contains("Database '" + dbName + "' already exists"))
                {
                    throw se;
                }
                Console.Write(" using existed database.");
            }
            sc.Close();
            Console.WriteLine();

            if (!File.Exists(@"ConnectionStrings.config"))
            {
                File.Copy(@"Template.ConnectionStrings.config", @"ConnectionStrings.config");
            }

            Console.WriteLine(@"Writing connection string in ""ConnectionStrings.config""");
            // set Rhetos to point to new database
            FileReplaceHelper.ReplaceWithRegex(@"ConnectionStrings.config"
                                               , @"<add.*?name=""ServerConnectionString""(.|\n)*?/>"
                                               , @"<add connectionString=""Data Source=" + args[0] + ";Initial Catalog=" + args[1] + @";Integrated Security=SSPI;"" name=""ServerConnectionString"" providerName=""Rhetos.MsSql"" />"
                                               , "Not valid ConnectionStrings.config file.");
        }
예제 #2
0
        static void Main(string[] args)
        {
            foreach (var a in args)
            {
                Console.WriteLine(a);
            }

            CreateAndSetDatabaseCliOptions options;

            try
            {
                options = CliOptionsParser.Parse <CreateAndSetDatabaseCliOptions>(args);
            }
            catch (ArgumentException ex)
            {
                Console.WriteLine(ex.Message);
                CreateAndSetDatabaseCliOptions.ShowHelp();
                return;
            }

            if (options.Help)
            {
                CreateAndSetDatabaseCliOptions.ShowHelp();
                return;
            }

            string serverName = options.ServerName;
            string dbName     = options.DatabaseName;
            string sqlMasterConnectionString = "server=" + serverName + ";database=master;Integrated Security=SSPI;";
            string rhetosConnectionString    = @"Data Source=" + serverName + ";Initial Catalog=" + dbName + @";Integrated Security=SSPI;";

            if (!options.UseSSPI)
            {
                sqlMasterConnectionString = "server=" + serverName + ";database=master;User ID=" + options.UserId + ";Password="******";";
                rhetosConnectionString    = @"Data Source=" + serverName + ";Initial Catalog=" + dbName + @";User ID=" + options.UserId + ";Password="******";";
            }

            SqlConnection sc = new SqlConnection(sqlMasterConnectionString);

            sc.Open();
            SqlCommand com = new SqlCommand("CREATE DATABASE " + dbName, sc);

            Console.Write("Preparing database ...");
            try
            {
                // Connection to remote server (e.g. Azure DB) can take very long time,
                // so CommandTimeout is set to 5 minutes.
                com.CommandTimeout = 300;
                com.ExecuteNonQuery();
                Console.Write(" database [" + serverName + "." + dbName + "] created.");
            }
            catch (SqlException se) {
                if (!se.Message.Contains("Database '" + dbName + "' already exists"))
                {
                    throw se;
                }
                Console.Write(" using existed database.");
            }
            sc.Close();
            Console.WriteLine();

            string appPath = AppDomain.CurrentDomain.BaseDirectory;
            string connectionStringConfigPath = Path.Combine(appPath, @"ConnectionStrings.config");

            if (!File.Exists(connectionStringConfigPath))
            {
                File.Copy(Path.Combine(appPath, @"Template.ConnectionStrings.config"), connectionStringConfigPath);
            }

            Console.WriteLine(@"Writing connection string in ""ConnectionStrings.config""");
            // set Rhetos to point to new database
            FileReplaceHelper.ReplaceWithRegex(connectionStringConfigPath
                                               , @"<add.*?name=""ServerConnectionString""(.|\n)*?/>"
                                               , @"<add connectionString=""" + rhetosConnectionString + @""" name=""ServerConnectionString"" providerName=""Rhetos.MsSql"" />"
                                               , "Not valid ConnectionStrings.config file.");
        }