public void InitialiseDb(DbConfiguration dbConfig, DeploymentConfiguration deployConfig) { Console.WriteLine("Connecting to the Database..."); SqlConnection connection = new SqlConnection(dbConfig.GetConnectionString()); try { EfDataContext efContext = new EfDataContext(connection); if (deployConfig.CreateAdmin) { if (string.IsNullOrWhiteSpace(deployConfig.AdminPassword)) { Console.WriteLine("An Administrator password was not specified."); Console.WriteLine("Skipping admin creation because of this."); } else { //create an admin user } } } catch(SqlException se) { Exit(1, "A problem occured whilst querying the Database:\n" + se.Message); } //more stuff here }
static void Main(string[] args) { DbConfiguration dbConfig = new DbConfiguration(); DeploymentConfiguration deployConfig = new DeploymentConfiguration(); //TODO: Sane arguments (-dbuser=foo, -DataContext=nh etc). For now we will use a pre-set configuration if (args.Count() < 1) { Exit(0, PrintHelp()); } foreach (string a in args) { string[] arg = a.Split(new char['='], 2); arg[0] = arg[0].Substring(1); //remove '-' switch (arg[0].ToLower()) { case "server": dbConfig.Server = arg[1]; break; case "database": case "db": dbConfig.Database = arg[1]; break; case "trustedconnection": if (string.IsNullOrEmpty(dbConfig.Username) && string.IsNullOrEmpty(dbConfig.Password)) { dbConfig.UseWindowsAuth = true; break; } if (arg.Count() < 2) { Exit(0, "You cannot use both Trusted and SQL Authentication!"); } Exit(0); break; case "username": if (arg.Count() < 2) { Exit(0, "You must provide a username!"); } if (!dbConfig.UseWindowsAuth) { dbConfig.Username = arg[1]; break; } if (arg.Count() < 2) { Exit(0, "You cannot use both Trusted and SQL Authentication!"); } break; case "password": if (arg.Count() < 2) { Exit(0, "You must provide a password!"); } if (!dbConfig.UseWindowsAuth) { dbConfig.Password = arg[1]; break; } if (arg.Count() < 2) { Exit(0, "You cannot use both Trusted and SQL Authentication!"); } break; case "createadmin": deployConfig.CreateAdmin = true; break; case "adminpassword": if (arg.Count() < 2) { Exit(0, "You must provide a password!"); } deployConfig.AdminPassword = arg[1]; break; case "shell": Exit(0, "WIP!!"); break; default: //invalid argument Console.WriteLine("The argument " + arg + " is invalid."); PrintHelp(); Exit(); break; } } //Verify the Database Configuration if (string.IsNullOrWhiteSpace(dbConfig.Server) || string.IsNullOrWhiteSpace(dbConfig.Database)) { Exit(0, "You must provide a Server and Database!"); } if (!dbConfig.UseWindowsAuth) { if (string.IsNullOrWhiteSpace(dbConfig.Username)) { Exit(0, "A Username must be provided when using SQL Server Authentication!"); } if (string.IsNullOrWhiteSpace(dbConfig.Password)) { Exit(0, "A Password must be provided when using SQL Server Authentication!"); } } Exit(); }
static void Main(string[] args) { DbConfiguration dbConfig = new DbConfiguration(); DeploymentConfiguration deployConfig = new DeploymentConfiguration(); //TODO: Sane arguments (-dbuser=foo, -DataContext=nh etc). For now we will use a pre-set configuration if (args.Count() < 1) Exit(0, PrintHelp()); foreach (string a in args) { string[] arg = a.Split(new char['='], 2); arg[0] = arg[0].Substring(1); //remove '-' switch (arg[0].ToLower()) { case "server": dbConfig.Server = arg[1]; break; case "database": case "db": dbConfig.Database = arg[1]; break; case "trustedconnection": if (string.IsNullOrEmpty(dbConfig.Username) && string.IsNullOrEmpty(dbConfig.Password)) { dbConfig.UseWindowsAuth = true; break; } if (arg.Count() < 2) Exit(0, "You cannot use both Trusted and SQL Authentication!"); Exit(0); break; case "username": if (arg.Count() < 2) Exit(0, "You must provide a username!"); if (!dbConfig.UseWindowsAuth) { dbConfig.Username = arg[1]; break; } if (arg.Count() < 2) Exit(0, "You cannot use both Trusted and SQL Authentication!"); break; case "password": if (arg.Count() < 2) Exit(0, "You must provide a password!"); if (!dbConfig.UseWindowsAuth) { dbConfig.Password = arg[1]; break; } if (arg.Count() < 2) Exit(0, "You cannot use both Trusted and SQL Authentication!"); break; case "createadmin": deployConfig.CreateAdmin = true; break; case "adminpassword": if (arg.Count() < 2) Exit(0, "You must provide a password!"); deployConfig.AdminPassword = arg[1]; break; case "shell": Exit(0, "WIP!!"); break; default: //invalid argument Console.WriteLine("The argument " + arg + " is invalid."); PrintHelp(); Exit(); break; } } //Verify the Database Configuration if (string.IsNullOrWhiteSpace(dbConfig.Server) || string.IsNullOrWhiteSpace(dbConfig.Database)) Exit(0, "You must provide a Server and Database!"); if (!dbConfig.UseWindowsAuth) { if (string.IsNullOrWhiteSpace(dbConfig.Username)) Exit(0, "A Username must be provided when using SQL Server Authentication!"); if (string.IsNullOrWhiteSpace(dbConfig.Password)) Exit(0, "A Password must be provided when using SQL Server Authentication!"); } Exit(); }