From 4210a1fc9f4b001d20fea893fc61cc83890f636f Mon Sep 17 00:00:00 2001 From: Sergej Derjabkin Date: Fri, 28 Sep 2012 17:33:13 +0200 Subject: [PATCH] - SQL Server authentication with username and password --- src/SqlBaseline/Configuration.cs | 56 +++++++++----- src/SqlBaseline/Program.cs | 122 ++++++++++++++++--------------- 2 files changed, 103 insertions(+), 75 deletions(-) diff --git a/src/SqlBaseline/Configuration.cs b/src/SqlBaseline/Configuration.cs index ef25a64..75b3eba 100644 --- a/src/SqlBaseline/Configuration.cs +++ b/src/SqlBaseline/Configuration.cs @@ -1,18 +1,40 @@ -namespace SqlBaseline -{ - public class Configuration - { - public string ServerName { get; internal set; } - public string DatabaseName { get; internal set; } - public string OutputFolder { get; internal set; } - - public string DatabaseConnection { get { return string.Format("Data Source={0};Initial Catalog={1};Integrated security=true", ServerName, DatabaseName); } } - - public bool IsValid - { - get { return !string.IsNullOrEmpty(ServerName) && - !string.IsNullOrEmpty(DatabaseName) && - !string.IsNullOrEmpty(OutputFolder); } - } - } +using System.Data.SqlClient; +namespace SqlBaseline +{ + public class Configuration + { + public string ServerName { get; internal set; } + public string DatabaseName { get; internal set; } + public string OutputFolder { get; internal set; } + public string UserName { get; internal set; } + public string Password { get; internal set; } + public string DatabaseConnection + { + get + { + + SqlConnectionStringBuilder csb = new SqlConnectionStringBuilder(); + csb.DataSource = ServerName; + csb.InitialCatalog = DatabaseName; + if (!string.IsNullOrEmpty(UserName)) + { + csb.UserID = UserName; + csb.Password = Password; + } + else + csb.IntegratedSecurity = true; + return csb.ConnectionString; + } + } + + public bool IsValid + { + get + { + return !string.IsNullOrEmpty(ServerName) && + !string.IsNullOrEmpty(DatabaseName) && + !string.IsNullOrEmpty(OutputFolder); + } + } + } } \ No newline at end of file diff --git a/src/SqlBaseline/Program.cs b/src/SqlBaseline/Program.cs index e51e49c..eddf38d 100644 --- a/src/SqlBaseline/Program.cs +++ b/src/SqlBaseline/Program.cs @@ -1,52 +1,52 @@ -using System; -using System.Configuration; -using System.Linq; -using SqlBaseline.Commandline.Options; -using SqlBaseline.Output; - -namespace SqlBaseline -{ - class Program - { - static void Main(string[] args) - { - if (ConfigurationManager.ConnectionStrings.Count == 0) return; - - var conn = new Configuration(); - var optionSet = ParseCommandLineOptions(conn); - - try - { - var app = new Application(conn); - - optionSet.Parse(args); - if(!conn.IsValid) - { - throw new OptionException("missing parameters",""); - } - - app.BuildEntities(); - app.AddCodeToEntities(); - - foreach (var writer in app - .TheObjects() - .Select(item => new FileWriter(item.ThingToTemplate, conn.OutputFolder))) - { - writer.DoWrite(); - } - } - catch (OptionException optionException) - { - Console.WriteLine(optionException.Message); - ShowTheHelp(optionSet); - } - } - - private static void ShowTheHelp(OptionSet optionSet) - { - optionSet.WriteOptionDescriptions(Console.Out); - } - +using System; +using System.Configuration; +using System.Linq; +using SqlBaseline.Commandline.Options; +using SqlBaseline.Output; + +namespace SqlBaseline +{ + class Program + { + static void Main(string[] args) + { + if (ConfigurationManager.ConnectionStrings.Count == 0) return; + + var conn = new Configuration(); + var optionSet = ParseCommandLineOptions(conn); + + try + { + var app = new Application(conn); + + optionSet.Parse(args); + if (!conn.IsValid) + { + throw new OptionException("missing parameters", ""); + } + + app.BuildEntities(); + app.AddCodeToEntities(); + + foreach (var writer in app + .TheObjects() + .Select(item => new FileWriter(item.ThingToTemplate, conn.OutputFolder))) + { + writer.DoWrite(); + } + } + catch (OptionException optionException) + { + Console.WriteLine(optionException.Message); + ShowTheHelp(optionSet); + } + } + + private static void ShowTheHelp(OptionSet optionSet) + { + optionSet.WriteOptionDescriptions(Console.Out); + } + private static OptionSet ParseCommandLineOptions(Configuration configuration) { var optionSet = new OptionSet() @@ -59,14 +59,20 @@ private static OptionSet ParseCommandLineOptions(Configuration configuration) o => configuration.ServerName = o) .Add("o=|output=", "The location to write the generated files.", - o => configuration.OutputFolder = o); + o => configuration.OutputFolder = o) + .Add("u=|user=", + "Username for the server to use", + o=>configuration.UserName = o) + .Add("p=|password=", + "Password for the server to user", + o=>configuration.Password = o); return optionSet; - } - - private static void ShowHelp(string option) - { - if (option != null) throw new OptionException("Help", ""); - } - } -} + } + + private static void ShowHelp(string option) + { + if (option != null) throw new OptionException("Help", ""); + } + } +}