Search code examples
c#sqlms-accessoledb

How to add rich text column to Access database table using C# and OleDB?


in an Access database we can set Text Format attribute of a Long Text column to either Plain Text or Rich Text.

How can we add a Rich text column to a table programmatically in C# using OleDb API?


Solution

  • How can we add a Rich text column to a table programmatically in C# using OleDb API?

    We can't do it using OleDb. We need to use Access DAO for that:

    // 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["MyTable"];  // existing table
    var fld = new Field();
    fld.Name = "MyRichMemo";
    fld.Type = (short) DataTypeEnum.dbMemo;
    tbd.Fields.Append(fld);
    // now set "Text Format" property to "Rich Text"
    fld.Properties.Append(fld.CreateProperty("TextFormat", DataTypeEnum.dbByte, 1));
    db.Close();