1
votes

I am using the OLEDB driver (Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=dBase IV) to work with a DBF table. I am running into an issue when I attempt to create a primary key on an existing column. I have been through several variations of syntax with no success.

What is the correct syntax for creating a primary key using OLEDB against a DBF table?

1

1 Answers

1
votes

Try this:

using (OleDbConnection cn = new OleDbConnection("Provider=Microsoft.Jet..."))
using (OleDbCommand cmd = new OleDbCommand("ALTER TABLE MyTable ADD CONSTRAINT idxMyTable PRIMARY KEY (MyColumn)", cn))
{
  cn.Open();
  cmd.ExecuteNonQuery();
}

Of course, you have to make sure the index doesn't already exist nor the values in the column do not currently violate the proposed primary key, etc.