ADOX multi-key primary key in C#

  • Thread starter Thread starter Scooter
  • Start date Start date
S

Scooter

Since I spent a few hours figuring this out, I figured
I'd post it for the record.

To add a composite (multiple column) primary key to an Access
database using ADOX and C#:

Table filesTable = new Table();
filesTable.Name = FILES_TABLE;
filesTable.Columns.Append(HOST_ID_COL, DataTypeEnum.adVarWChar, 100);
filesTable.Columns.Append(PATH_COL, DataTypeEnum.adVarWChar, 250);
filesTable.Columns.Append(PATH2_COL, DataTypeEnum.adVarWChar, 250);
filesTable.Columns.Append(NAME_COL, DataTypeEnum.adVarWChar, 200);

filesTable.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary,
HOST_ID_COL, null, null);
filesTable.Keys["PrimaryKey"].Columns.Append(PATH_COL,
DataTypeEnum.adVarWChar, 250);
filesTable.Keys["PrimaryKey"].Columns.Append(PATH_COL,
DataTypeEnum.adVarWChar, 250);
catalog.Tables.Append(filesTable);


There's the trick right there. Create the primary key specifying a
single column, then retrieve the key and add the additional columns.
It's so simple...

--Scott
 
* (e-mail address removed) (Scooter) scripsit:
To add a composite (multiple column) primary key to an Access
database using ADOX and C#:

Table filesTable = new Table();
filesTable.Name = FILES_TABLE;
filesTable.Columns.Append(HOST_ID_COL, DataTypeEnum.adVarWChar, 100);
filesTable.Columns.Append(PATH_COL, DataTypeEnum.adVarWChar, 250);
filesTable.Columns.Append(PATH2_COL, DataTypeEnum.adVarWChar, 250);
filesTable.Columns.Append(NAME_COL, DataTypeEnum.adVarWChar, 200);

filesTable.Keys.Append("PrimaryKey", KeyTypeEnum.adKeyPrimary,
HOST_ID_COL, null, null);
filesTable.Keys["PrimaryKey"].Columns.Append(PATH_COL,
DataTypeEnum.adVarWChar, 250);
filesTable.Keys["PrimaryKey"].Columns.Append(PATH_COL,
DataTypeEnum.adVarWChar, 250);
catalog.Tables.Append(filesTable);

Your question doesn't seem to be related to .NET Windows Forms
programming, so I suggest to post it to the ADO.NET group:

<URL:
Web interface:

<URL:http://msdn.microsoft.com/newsgroups/?dg=microsoft.public.dotnet.framework.adonet>
 
Back
Top