Link to home
Start Free TrialLog in
Avatar of Rob4077
Rob4077Flag for Australia

asked on

Is it possible to connect to a DBF file whose path is entered on a form?

I need to write a rather small application that will enable a user to select a file path that contains some dbf files before connecting to those files (I need to connect exclusively - no other application is allowed to be using those files while this one has them) and then opening another window to allow some additions to be made. In MS Access I think I could manage this relatively easily but I would like to use it as a learning exercise in VB.Net

My question is: How would I connect to a DBF file whose path is entered on a form?
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

The answer depends on how you need it, and what you are going to do once you get the data.  You can use an OleDbConnectionStringBuilder, set properties on the builder, and get a valid connection string.

http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbconnectionstringbuilder.aspx

using System.Data.OleDb;

class Program
{
    static void Main(string[] args)
    {
        OleDbConnectionStringBuilder builder = new OleDbConnectionStringBuilder();
        builder.ConnectionString = @"Data Source=C:\Sample.mdb";

        // Call the Add method to explicitly add key/value
        // pairs to the internal collection.
        builder.Add("Provider", "Microsoft.Jet.Oledb.4.0");
        builder.Add("Jet OLEDB:Database Password", "MyPassword!");
        builder.Add("Jet OLEDB:System Database", @"C:\Workgroup.mdb");

        // Set up row-level locking.
        builder.Add("Jet OLEDB:Database Locking Mode", 1);

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

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

        // Pass the OleDbConnectionStringBuilder an existing 
        // connection string, and you can retrieve and
        // modify any of the elements.
        builder.ConnectionString =
            "Provider=DB2OLEDB;Network Transport Library=TCPIP;" +
            "Network Address=192.168.0.12;Initial Catalog=DbAdventures;" +
            "Package Collection=SamplePackage;Default Schema=SampleSchema;";

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

        // Modify existing items.
        builder["Package Collection"] = "NewPackage";
        builder["Default Schema"] = "NewSchema";

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

        // 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 value, if 
        // necessary.
        builder["User ID"] = "SampleUser";
        builder["Password"] = "SamplePassword";
        Console.WriteLine(builder.ConnectionString);

        Console.WriteLine("Press Enter to finish.");
        Console.ReadLine();
    }
}

Open in new window

Avatar of Rob4077

ASKER

Thanks for the comments and solution.
You said >>The answer depends on how you need it, and what you are going to do once you get the data.<<
Once I have connected a new form will need to list the records in the file. I need to give the user the ability to edit existing records or press a button alongside a selected record and use that record as a template to add a new record. The user then simply makes a few additional changes to the new record. When the user has finished adding/editing records I need to sort the table by a key field and save it before closing the application.
Is this going to be asking too much?
Avatar of Rob4077

ASKER

BTW, your instructions connect to an mdb file. Can VB.Net connect to a dbf file?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Rob4077

ASKER

Brilliant. thank you very much!!!!