private static string GetValue(this OdbcConnectionStringBuilder odbc, string key)
        {
            object oValue;

            odbc.TryGetValue(key, out oValue);
            return(oValue as string);
        }
Example #2
0
    private static void DisplayValue(OdbcConnectionStringBuilder builder, string key)
    {
        object value = null;

        // Although TryGetValue handles missing keys,
        // it does not handle passing in a null (Nothing in Visual Basic)
        // key. This example traps for that particular error, but
        // throws any other unknown exceptions back out to the
        // caller.
        try
        {
            if (builder.TryGetValue(key, out value))
            {
                Console.WriteLine("{0}='{1}'", key, value);
            }
            else
            {
                Console.WriteLine("Unable to retrieve value for '{0}'", key);
            }
        }
        catch (ArgumentNullException ex)
        {
            Console.WriteLine("Unable to retrieve value for null key.");
        }
    }
        public void TryGetValue_Keyword_Null()
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
            object value = null;

            try {
                builder.TryGetValue(null, out value);
                Assert.Fail("#1");
            } catch (ArgumentNullException ex) {
                Assert.AreEqual(typeof(ArgumentNullException), ex.GetType(), "#2");
                Assert.IsNull(ex.InnerException, "#3");
                Assert.IsNotNull(ex.Message, "#4");
                Assert.AreEqual("keyword", ex.ParamName, "#5");
            }
        }
        public override ValidationResult Validate(object value, CultureInfo cultureInfo)
        {
            string s = value as string;

            if (string.IsNullOrEmpty(s))
            {
                if (Required)
                {
                    return(new ValidationResult(false, "Field must not be empty"));
                }
                else
                {
                    return(ValidationResult.ValidResult);
                }
            }

            try
            {
                var    a             = new OdbcConnectionStringBuilder(s);
                var    missingParams = new List <string>();
                object val           = null;
                foreach (var p in RequiredParameters ?? new string[0])
                {
                    if (!a.TryGetValue(p, out val) || string.IsNullOrEmpty(val as string))
                    {
                        missingParams.Add(p);
                    }
                }
                if (missingParams.Any())
                {
                    return(new ValidationResult(false, $"Parameters missing: {string.Join(",", missingParams)}"));
                }
                return(ValidationResult.ValidResult);
            }
            catch
            {
                return(new ValidationResult(false, "ODBC Connection string is invalid"));
            }
        }
        public void TryGetValue()
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();
            object value = null;

            builder ["DriverID"] = "790";
            builder.Add("Server", "C:\\");
            Assert.IsTrue(builder.TryGetValue("DriverID", out value), "#A1");
            Assert.AreEqual("790", value, "#A2");
            Assert.IsTrue(builder.TryGetValue("SERVER", out value), "#B1");
            Assert.AreEqual("C:\\", value, "#B2");
            Assert.IsFalse(builder.TryGetValue(string.Empty, out value), "#C1");
            Assert.IsNull(value, "#C2");
            Assert.IsFalse(builder.TryGetValue("a;", out value), "#D1");
            Assert.IsNull(value, "#D2");
            Assert.IsFalse(builder.TryGetValue("\r", out value), "#E1");
            Assert.IsNull(value, "#E2");
            Assert.IsFalse(builder.TryGetValue(" ", out value), "#F1");
            Assert.IsNull(value, "#F2");
            Assert.IsFalse(builder.TryGetValue("doesnotexist", out value), "#G1");
            Assert.IsNull(value, "#G2");
            Assert.IsTrue(builder.TryGetValue("Driver", out value), "#H1");
            Assert.AreEqual(string.Empty, value, "#H2");
            Assert.IsTrue(builder.TryGetValue("Dsn", out value), "#I1");
            Assert.AreEqual(string.Empty, value, "#I2");
            builder ["Driver"] = "SQL Server";
            Assert.IsTrue(builder.TryGetValue("Driver", out value), "#J1");
            Assert.AreEqual("SQL Server", value, "#J2");
            builder.Dsn = "AdventureWorks";
            Assert.IsTrue(builder.TryGetValue("Dsn", out value), "#K1");
            Assert.AreEqual("AdventureWorks", value, "#K2");
        }