KFP
KFP

Reputation: 719

How to set up a connection to a database for a program that will be distributed

I have a program that currently points to a database on my local drive. I will be placing the dataset on a network drive on a server and then distributing the program to other compters. How am I to set up a connection that will work on other compuers(where the network drive letter may be different)? I have tried seting this up through the App.Config file as well as using differnent Data Source configurations in the OleDbConnection. This is shortened version of my connection:

string strSQL = "INSERT INTO TestTable(Name1, Address) VALUES(@FirstName, @Address)";
        OleDbConnection myConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\TEMP\\TestDatabase.accdb");
        OleDbCommand myCommand = new OleDbCommand(strSQL, myConnection);
        myCommand.Parameters.AddWithValue("@FirstName", txtName.Text);
        myCommand.Parameters.AddWithValue("@Address", txtAddress.Text);

        try
        {
            myConnection.Open();
            myCommand.ExecuteNonQuery();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            myConnection.Close();
        }

Upvotes: 0

Views: 240

Answers (1)

Gord Thompson
Gord Thompson

Reputation: 123549

Mike's comment above re: UNC paths makes good sense. Just use

Data Source=\\servername\sharename\path\to\data\file.accdb;

Upvotes: 1

Related Questions