private readonly dynamic _connection; // M.D.S.SqlConnection /// <summary> /// Creates a new <see cref="SqlConnection"/> instance for the specified /// context and database name, logging server messages via the specified /// cmdlet. /// </summary> /// <param name="context"> /// An object containing information necessary to connect to a database. /// If not provided, the constructor will use a context with default /// property values. /// </param> /// <param name="databaseName"> /// The name of the database to which to connect. If not provided, the /// constructor connects to the default database for the context. /// </param> /// <param name="cmdlet"> /// The cmdlet whose /// <see cref="Cmdlet.WriteHost(string, bool, ConsoleColor?, ConsoleColor?)"/> /// and /// <see cref="System.Management.Automation.Cmdlet.WriteWarning(string)"/> /// methods will be used to print messges received from the server. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="cmdlet"/> is <see langword="null"/>. /// </exception> /// <exception cref="System.Data.Common.DbException"> /// A connection-level error occurred while opening the connection. /// </exception> internal SqlConnection(SqlContext?context, string?databaseName, Cmdlet cmdlet) { const SqlClientVersion Version = PSqlClient.Version; if (cmdlet is null) { throw new ArgumentNullException(nameof(cmdlet)); } context ??= new SqlContext(); var client = PSqlClient.Instance; var connectionString = context.GetConnectionString(databaseName, Version, true); var credential = context.Credential; var writeInformation = new Action <string>(s => cmdlet.WriteHost(s)); var writeWarning = new Action <string>(s => cmdlet.WriteWarning(s)); var passCredentialSeparately = !credential.IsNullOrEmpty() && !context.ExposeCredentialInConnectionString; _connection = passCredentialSeparately ? client.Connect( connectionString, credential !.UserName, credential !.Password, writeInformation, writeWarning ) : client.Connect( connectionString, writeInformation, writeWarning ); }
/// <summary> /// Initializes a new <see cref="SqlConnectionStringBuilder"/> instance /// for the specified SqlClient version. /// </summary> /// <param name="version"> /// The SqlClient version against which the builder should maintain /// compatibility. /// </param> public SqlConnectionStringBuilder(SqlClientVersion version = SqlClientVersion.Legacy) { _builder = new(); Version = version; }