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?
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?
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();
Answering the question: "How to add a rich text column to an Access table".
Access 2010 and above allows rich text to be saved in a memo. Below is the c# OleDB code I have used to add a memo column to an Access table.
public OleDbConnection myAccessConnection = new OleDbConnection();
myAccessConnection.ConnectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\myAccessDb.accdb";
OleDbCommand sqlCommand = new OleDbCommand();
myAccessConnection.Open();
sqlCommand.Connection = myAccessConnection;
sqlCommand.CommandText = "ALTER TABLE [MyTableInMyAccessDb] ADD MyMemo MEMO";
sqlCommand.ExecuteNonQuery();
myAccessConnection.Close();