Exemple #1
0
        public SqlDb(string connection, SqlSyntax syntax, string userName = null, ConnectionSource connectionSource = ConnectionSource.ConfigFile)
        {
            _syntax = syntax;

            UserName = userName;

            switch (connectionSource)
            {
            case ConnectionSource.ConfigFile:
                try
                {
                    _connectionString = ConfigurationManager.ConnectionStrings[connection].ConnectionString;
                    ConnectionName    = connection;
                }
                catch (NullReferenceException)
                {
                    string fileName       = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
                    string allConnections = AllConnectionNames();
                    throw new NullReferenceException($"Connection string named '{connection}' was not found in {fileName}. These connection names are defined: {allConnections}");
                }
                break;

            case ConnectionSource.Literal:
                _connectionString = connection;
                break;
            }

            if (_connectionString.StartsWith("@"))
            {
                string name = _connectionString.Substring(1);
                _connectionString = ConnectionStringReference.Resolve(name);
            }
        }
Exemple #2
0
        public void CreateConnectionStringRefEncrypted()
        {
            string refFile = ConnectionStringReference.GetFilename("hello");

            if (File.Exists(refFile))
            {
                File.Delete(refFile);
            }

            const string sample       = "this is a sample connection string";
            var          settingsFile = ConnectionStringReference.Create("hello", sample, DataProtectionScope.CurrentUser);

            var connectionString = ConnectionStringReference.Resolve("hello");

            Assert.IsTrue(connectionString.Equals(sample));

            File.Delete(settingsFile);
        }