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

        // Call the Add method to explicitly add key/value
        // pairs to the internal collection.
        builder.Driver        = "Microsoft Excel Driver (*.xls)";
        builder["Dbq"]        = "C:\\Data.xls";
        builder["DriverID"]   = "790";
        builder["DefaultDir"] = "C:\\";

        // Dump the contents of the "filled-in" OdbcConnectionStringBuilder
        // to the Console window:
        DumpBuilderContents(builder);

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

        // Dump the contents of the newly emptied OdbcConnectionStringBuilder
        // to the console window.
        DumpBuilderContents(builder);

        Console.WriteLine("Press Enter to continue.");
        Console.ReadLine();
    }
Beispiel #2
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();
    }
        public void Clear()
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();

            builder ["Dbq"] = "C:\\Data.xls";
            builder.Driver  = "SQL Server";
            builder.Dsn     = "AdventureWorks";
            builder.Add("Port", "56");
            builder.Clear();
            Assert.AreEqual(string.Empty, builder.ConnectionString, "#1");
            Assert.IsFalse(builder.ContainsKey("Dbq"), "#2");
            Assert.AreEqual(string.Empty, builder.Driver, "#3");
            Assert.AreEqual(string.Empty, builder.Dsn, "#4");
            Assert.IsFalse(builder.ContainsKey("Port"), "#5");
        }
        public void Dsn()
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();

            Assert.IsTrue(builder.ContainsKey("Dsn"), "#A1");
            Assert.AreEqual(string.Empty, builder ["Dsn"], "#A2");
            Assert.AreEqual(string.Empty, builder.Dsn, "#A3");

            builder.Dsn = "myDsn";
            Assert.AreEqual("Dsn=myDsn", builder.ConnectionString, "#B1");
            Assert.IsTrue(builder.ContainsKey("Dsn"), "#B2");
            Assert.AreEqual("myDsn", builder ["Dsn"], "#B3");
            Assert.AreEqual("myDsn", builder.Dsn, "#B4");

            builder.Clear();

            builder ["Dsn"] = "myDsn";
            Assert.AreEqual("Dsn=myDsn", builder.ConnectionString, "#C1");
            Assert.IsTrue(builder.ContainsKey("Dsn"), "#C2");
            Assert.AreEqual("myDsn", builder ["Dsn"], "#C3");
            Assert.AreEqual("myDsn", builder.Dsn, "#C4");
        }
Beispiel #5
0
 private void CancelButton_Click(object sender, EventArgs e)
 {
     // Clear the builder so that the resulting int returned to the caller is 0.
     Builder.Clear();
     Close();
 }
        public void Keys()
        {
            OdbcConnectionStringBuilder builder;
            ICollection keys;

            object [] keylist;

            builder = new OdbcConnectionStringBuilder();
            keys    = builder.Keys;
            Assert.IsNotNull(keys, "#A1");
            Assert.AreEqual(2, keys.Count, "#A2");
            keylist = new object [keys.Count];
            keys.CopyTo(keylist, 0);
            Assert.AreEqual(2, keylist.Length, "#A3");
            Assert.AreEqual("Dsn", keylist [0], "#A4");
            Assert.AreEqual("Driver", keylist [1], "#A5");

            builder = new OdbcConnectionStringBuilder("Database=test;Driver=SQL Server;dsn=AdventureWorks");
            keys    = builder.Keys;
            Assert.IsNotNull(keys, "#B1");
            Assert.AreEqual(3, keys.Count, "#B2");
            keylist = new object [keys.Count];
            keys.CopyTo(keylist, 0);
            Assert.AreEqual(3, keylist.Length, "#B3");
            Assert.AreEqual("Dsn", keylist [0], "#B4");
            Assert.AreEqual("Driver", keylist [1], "#B5");
            Assert.AreEqual("database", keylist [2], "#B6");

            builder = new OdbcConnectionStringBuilder("Driver=SQL Server;dsn=AdventureWorks;Database=test;Port=");
            keys    = builder.Keys;
            Assert.IsNotNull(keys, "#C1");
            Assert.AreEqual(3, keys.Count, "#C2");
            keylist = new object [keys.Count];
            keys.CopyTo(keylist, 0);
            Assert.AreEqual(3, keylist.Length, "#C3");
            Assert.AreEqual("Dsn", keylist [0], "#C4");
            Assert.AreEqual("Driver", keylist [1], "#C5");
            Assert.AreEqual("database", keylist [2], "#C6");

            builder = new OdbcConnectionStringBuilder();
            builder ["DataBase"] = "test";
            builder.Driver       = "SQL Server";
            builder ["User"]     = "******";
            builder ["porT"]     = "25";
            keys = builder.Keys;
            Assert.IsNotNull(keys, "#D1");
            Assert.AreEqual(5, keys.Count, "#D2");
            keylist = new object [keys.Count];
            keys.CopyTo(keylist, 0);
            Assert.AreEqual(5, keylist.Length, "#D3");
            Assert.AreEqual("Dsn", keylist [0], "#D4");
            Assert.AreEqual("Driver", keylist [1], "#D5");
            Assert.AreEqual("DataBase", keylist [2], "#D6");
            Assert.AreEqual("User", keylist [3], "#D7");
            Assert.AreEqual("porT", keylist [4], "#D8");

            builder.Clear();

            keys = builder.Keys;
            Assert.IsNotNull(keys, "#E1");
            Assert.AreEqual(2, keys.Count, "#E2");
            keylist = new object [keys.Count];
            keys.CopyTo(keylist, 0);
            Assert.AreEqual(2, keylist.Length, "#E3");
            Assert.AreEqual("Dsn", keylist [0], "#E4");
            Assert.AreEqual("Driver", keylist [1], "#E5");
        }
        public void Driver()
        {
            OdbcConnectionStringBuilder builder = new OdbcConnectionStringBuilder();

            Assert.IsTrue(builder.ContainsKey("Driver"), "#A1");
            Assert.AreEqual(string.Empty, builder ["Driver"], "#A2");
            Assert.AreEqual(string.Empty, builder.Driver, "#A3");

            builder.Driver = "SQL Server";
            Assert.AreEqual("Driver={SQL Server}", builder.ConnectionString, "#B1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#B2");
            Assert.AreEqual("SQL Server", builder ["Driver"], "#B3");
            Assert.AreEqual("SQL Server", builder.Driver, "#B4");

            builder.Clear();

            builder.Driver = "{SQL Server";
            Assert.AreEqual("Driver={{SQL Server}", builder.ConnectionString, "#C1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#C2");
            Assert.AreEqual("{SQL Server", builder ["Driver"], "#C3");
            Assert.AreEqual("{SQL Server", builder.Driver, "#C4");

            builder.Clear();

            builder.Driver = "{SQL Server}";
            Assert.AreEqual("Driver={SQL Server}", builder.ConnectionString, "#D1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#D2");
            Assert.AreEqual("{SQL Server}", builder ["Driver"], "#D3");
            Assert.AreEqual("{SQL Server}", builder.Driver, "#D4");

            builder.Clear();

            builder.Driver = string.Empty;
            Assert.AreEqual("Driver=", builder.ConnectionString, "#E1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#E2");
            Assert.AreEqual(string.Empty, builder ["Driver"], "#E3");
            Assert.AreEqual(string.Empty, builder.Driver, "#E4");

            builder.Clear();

            builder ["Driver"] = "SQL Server";
            Assert.AreEqual("Driver={SQL Server}", builder.ConnectionString, "#F1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#F2");
            Assert.AreEqual("SQL Server", builder ["Driver"], "#F3");
            Assert.AreEqual("SQL Server", builder.Driver, "#F4");

            builder.Clear();

            builder ["Driver"] = "{SQL Server";
            Assert.AreEqual("Driver={{SQL Server}", builder.ConnectionString, "#G1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#G2");
            Assert.AreEqual("{SQL Server", builder ["Driver"], "#G3");
            Assert.AreEqual("{SQL Server", builder.Driver, "#G4");

            builder.Clear();

            builder ["Driver"] = "{SQL Server}";
            Assert.AreEqual("Driver={SQL Server}", builder.ConnectionString, "#H1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#H2");
            Assert.AreEqual("{SQL Server}", builder ["Driver"], "#H3");
            Assert.AreEqual("{SQL Server}", builder.Driver, "#H4");

            builder.Clear();

            builder ["Driver"] = string.Empty;
            Assert.AreEqual("Driver=", builder.ConnectionString, "#I1");
            Assert.IsTrue(builder.ContainsKey("Driver"), "#I2");
            Assert.AreEqual(string.Empty, builder ["Driver"], "#I3");
            Assert.AreEqual(string.Empty, builder.Driver, "#I4");
        }