示例#1
0
        private static void CreateLoginIfNotExists(Server server, Database database)
        {
            Login login = null;
            if (!server.LoginExists(Configuration.LoginName))
            {
                Console.WriteLine("Creating Login {0}", Configuration.LoginName);
                login = server.CreateSqlLogin(Configuration.LoginName, Configuration.LoginPassword, Configuration.DatabaseName);
            }
            else
            {
                login = server.Logins[Configuration.LoginName];
            }

            if (!database.UserExists(Configuration.LoginName))
            {
                Console.WriteLine("Creating User {0} for Database {1}", login.Name, database.Name);
                var user = new User(database, Configuration.LoginName);
                user.Login = login.Name;
                user.Create();
                user.AddToRole("db_owner");
            }
            else
            {
                ConsoleHelper.WriteLine(ConsoleColor.Yellow, string.Format("User {0} already exists in database {1}, creation skiped", Configuration.LoginName, Configuration.DatabaseName));
            }
        }