I'm trying to create an Access file (.mdb) with C#. I'm exporting data from SQL for a legacy process. I got it working, the only problem I'm facing is setting columns to nullable.
Here's what I have (removed most columns for brevity):
private void CreateAndExportLegacyFile(DataTable data, string exportFilename)
{
var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;";
connectionString += "Data Source=" + exportFilename + ";Jet OLEDB:Engine Type=5";
var catalog = new Catalog();
catalog.Create(connectionString);
var table = new Table { Name = "Main" };
#region Column mapping
table.Columns.Append("ID", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("FIRST", DataTypeEnum.adVarWChar, 50);
table.Columns.Append("LAST", DataTypeEnum.adVarWChar, 50);
#endregion
foreach (Column column in table.Columns)
{
if (column.Name != "ID") column.Properties["Nullable"].Value = true;
}
catalog.Tables.Append(table);
Marshal.FinalReleaseComObject(table);
Marshal.FinalReleaseComObject(catalog.Tables);
Marshal.FinalReleaseComObject(catalog.ActiveConnection);
Marshal.FinalReleaseComObject(catalog);
}
I get the following error when trying to set the "Nullable" property: "Item cannot be found in the collection corresponding to the requested name or ordinal."
I've found conflicting reports of whether to use Required or Nullable, but I've tried both and get the same result.
Any idea why I'm not able to set the nullable property? I may just be lazy and default nulls to blank spaces when pulling the data, but would like to avoid that if possible.
Edit: As Hans mentioned in a comment, I had to use the Attributes property instead:
if (column.Name != "ID") column.Attributes = ColumnAttributesEnum.adColNullable;
Hans Passant answered this in a comment but never added it as an answer. Doing so now to mark this as answered. Had to use the Attributes property: