Reputation: 43
I have an MS Access MDB that has a link table from a .csv. I have to change the path so I must use the Linked Table Manager in order to update the path of the csv.
Is any way to do that programmatically using C#?
I have tried to use the help in this link, but it's about Sql and MsAccess. enter link description here
Thank you in advance.
Upvotes: 1
Views: 793
Reputation: 123664
Say you have an existing linked table named [Foo] that points to the following CSV file:
C:\Users\Gord\Desktop\foo.csv
Access stores the linking information in a TableDef object named "Foo" with the following properties:
.Name = "Foo"
.Connect = "Text;DSN=Foo Link Specification;FMT=Delimited;HDR=NO;IMEX=2;CharacterSet=437;ACCDB=YES;DATABASE=C:\Users\Gord\Desktop"
.SourceTableName = "foo.csv"
Notice that the DATABASE=
parameter of the .Connect
property indicates the folder in which the CSV file resides, and the .SourceTableName
property indicates the name of the CSV file.
If you move that file from C:\Users\Gord\Desktop
to C:\Users\Public
then all you need to do is update the DATABASE=
parameter of the .Connect
property with the new location:
// required COM reference: Microsoft Office 14.0 Access Database Engine Object Library
//
// using Microsoft.Office.Interop.Access.Dao; ...
var dbe = new DBEngine();
Database db = dbe.OpenDatabase(@"C:\Users\Public\Database1.accdb");
TableDef tbd = db.TableDefs["Foo"];
string oldConnect = tbd.Connect;
char[] delimiter = { ';' };
string[] connectParams = oldConnect.Split(delimiter);
for (int i = 0; i < connectParams.Length; i++)
{
if (connectParams[i].StartsWith("DATABASE=", StringComparison.InvariantCultureIgnoreCase))
{
connectParams[i] = @"DATABASE=C:\Users\Public";
break;
}
}
string newConnect = String.Join(Convert.ToString(delimiter[0]), connectParams);
tbd.Connect = newConnect;
tbd.RefreshLink();
db.Close()
Upvotes: 2