Beispiel #1
0
    static void Main()
    {
        OdbcConnectionStringBuilder builder =
            new OdbcConnectionStringBuilder();

        builder.Driver = "Microsoft Access Driver (*.mdb)";

        // Call the Add method to explicitly add key/value
        // pairs to the internal collection.
        builder.Add("Dbq", "C:\\info.mdb");
        builder.Add("Uid", "Admin");
        builder.Add("Pwd", "pass!word1");

        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();

        // Clear current values and reset known keys to their
        // default values.
        builder.Clear();

        // Pass the OdbcConnectionStringBuilder an existing
        // connection string, and you can retrieve and
        // modify any of the elements.
        builder.ConnectionString =
            "driver={IBM DB2 ODBC DRIVER};Database=SampleDB;" +
            "hostname=SampleServerName;port=SamplePortNum;" +
            "protocol=TCPIP;uid=Admin;pwd=pass!word1";

        Console.WriteLine("protocol = "
                          + builder["protocol"].ToString());
        Console.WriteLine();

        // Modify existing items.
        builder["uid"] = "NewUser";
        builder["pwd"] = "Pass@word2";

        // Call the Remove method to remove items from
        // the collection of key/value pairs.
        builder.Remove("port");

        // Note that calling Remove on a nonexistent item does not
        // throw an exception.
        builder.Remove("BadItem");
        Console.WriteLine(builder.ConnectionString);
        Console.WriteLine();

        // Setting the indexer adds the associated value, if
        // necessary.
        builder["NewKey"] = "newValue";
        Console.WriteLine(builder.ConnectionString);

        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }
Beispiel #2
0
        /// <inheritdoc/>
        protected override string GetConnectionStringWithLoginInfo(string userName, string password)
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder(ConnectionString);

            builder.Remove("uid");
            builder.Add("uid", userName);

            builder.Remove("pwd");
            builder.Add("pwd", password);

            return(builder.ToString());
        }
Beispiel #3
0
        private void EnableDisableActionButtons()
        {
            if (string.IsNullOrEmpty(textCloudID.Text) == false)
            {
                radioButtonDisabled.Checked      = false;
                radioEnabledNoValidation.Checked = false;
                radioEnabledNoHostname.Checked   = false;
                radioEnabledHostname.Checked     = false;
                radioEnabledFull.Checked         = false;
                groupSSL.Enabled = false;
                Builder.Remove("secure");

                textHostname.ResetText();
                Builder.Remove("server");
                numericUpDownPort.ResetText();
                Builder.Remove("port");
            }
            else
            {
                groupSSL.Enabled = true;
            }

            textCloudID.Enabled              = string.IsNullOrEmpty(textHostname.Text);
            textHostname.Enabled             = numericUpDownPort.Enabled
                                             = textCertificatePath.Enabled = certificatePathButton.Enabled
                                                                                 = string.IsNullOrEmpty(textCloudID.Text);

            if (isConnecting)
            {
                // If connecting, enable the button if we have a hostname.
                // This can be triggered by app connecting or FileDSN verifying the connection.
                saveButton.Enabled = string.IsNullOrEmpty(textHostname.Text) == false || string.IsNullOrEmpty(textCloudID.Text) == false;
            }
            else
            {
                // If editing a (User/System) DSN, enable the buton if both DSN name and hostname are available.
                saveButton.Enabled = string.IsNullOrEmpty(textName.Text) == false &&
                                     (string.IsNullOrEmpty(textHostname.Text) == false || string.IsNullOrEmpty(textCloudID.Text) == false);
            }
            testButton.Enabled = string.IsNullOrEmpty(textHostname.Text) == false || string.IsNullOrEmpty(textCloudID.Text) == false;
        }
        protected override void SetConnectionString(string value)
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder(value);

            FillDataSources();
            if (String.IsNullOrEmpty(value))
            {
                rbDsName.Checked = true;
            }
            else if (builder.ContainsKey("Dsn"))
            {
                string dsn = builder["Dsn"] as string;
                if (cbxDsName.Items.Contains(dsn))
                {
                    cbxDsName.SelectedIndex = cbxDsName.Items.IndexOf(dsn);
                    rbDsName.Checked        = true;
                }
                else
                {
                    rbConnectionString.Checked = true;
                }
            }

            if (builder.ContainsKey("uid"))
            {
                tbUserName.Text = builder["uid"] as string;
                builder.Remove("uid");
            }
            if (builder.ContainsKey("pwd"))
            {
                tbPassword.Text = builder["pwd"] as string;
                builder.Remove("pwd");
            }

            // display the string w/o uid, pwd
            tbConnectionString.Text = builder.ToString();

            // update the enabled state
            rbDsName_CheckedChanged(this, EventArgs.Empty);
        }
Beispiel #5
0
 static void TryRemove(OdbcConnectionStringBuilder builder,
                       string itemToRemove)
 {
     if (builder.Remove(itemToRemove))
     {
         Console.WriteLine("Removed '{0}'", itemToRemove);
     }
     else
     {
         Console.WriteLine("Unable to remove '{0}'", itemToRemove);
     }
     Console.WriteLine(builder.ConnectionString);
 }
        public void Remove_Keyword_Null()
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();

            try {
                builder.Remove(null);
                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");
            }
        }
        protected override string GetConnectionString()
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder(tbConnectionString.Text);

            if (!String.IsNullOrEmpty(tbUserName.Text))
            {
                builder.Add("uid", tbUserName.Text);
            }
            else
            {
                builder.Remove("uid");
            }

            if (!String.IsNullOrEmpty(tbPassword.Text))
            {
                builder.Add("pwd", tbPassword.Text);
            }
            else
            {
                builder.Remove("pwd");
            }

            return(builder.ToString());
        }
        public void Remove()
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();

            Assert.IsFalse(builder.Remove("Dsn"), "#A1");
            Assert.IsFalse(builder.Remove("Driver"), "#A2");
            builder.Add("DriverID", "790");
            builder ["DefaultDir"] = "C:\\";
            Assert.IsTrue(builder.Remove("DriverID"), "#B1");
            Assert.IsFalse(builder.ContainsKey("DriverID"), "#B2");
            Assert.IsFalse(builder.Remove("DriverID"), "#B3");
            Assert.IsFalse(builder.ContainsKey("DriverID"), "#B4");
            Assert.IsTrue(builder.Remove("defaulTdIr"), "#B5");
            Assert.IsFalse(builder.ContainsKey("DefaultDir"), "#B6");
            Assert.IsFalse(builder.Remove("defaulTdIr"), "#B7");
            Assert.IsFalse(builder.Remove("userid"), "#B8");
            Assert.IsFalse(builder.Remove(string.Empty), "#B9");
            Assert.IsFalse(builder.Remove("\r"), "#B10");
            Assert.IsFalse(builder.Remove("a;"), "#B11");
            builder.Dsn = "myDsn";
            Assert.IsTrue(builder.Remove("dSn"), "#C1");
            Assert.IsTrue(builder.ContainsKey("dSn"), "#C2");
            Assert.IsTrue(builder.ContainsKey("Dsn"), "#C3");
            Assert.AreEqual(string.Empty, builder.Dsn, "#C4");
            Assert.IsFalse(builder.Remove("Dsn"), "#C5");
            builder.Driver = "SQL Server";
            Assert.IsTrue(builder.Remove("driVer"), "#D1");
            Assert.IsTrue(builder.ContainsKey("driVer"), "#D2");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#D3");
            Assert.AreEqual(string.Empty, builder.Driver, "#D4");
            Assert.IsFalse(builder.Remove("Driver"), "#D5");
            builder ["Dsn"] = "myDsn";
            Assert.IsTrue(builder.Remove("Dsn"), "#E1");
            Assert.IsTrue(builder.ContainsKey("Dsn"), "#E2");
            Assert.AreEqual(string.Empty, builder.Dsn, "#E3");
            Assert.IsFalse(builder.Remove("Dsn"), "#E4");
            builder ["Driver"] = "SQL Server";
            Assert.IsTrue(builder.Remove("Driver"), "#F1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#F2");
            Assert.AreEqual(string.Empty, builder.Driver, "#F3");
            Assert.IsFalse(builder.Remove("Driver"), "#F4");
        }