0

I used this code for create an access database.

public void CreateAccessAssetFile(string DataSourcePath, string fileName)
{
    if (File.Exists(DataSourcePath + fileName))
            File.Delete(DataSourcePath + fileName);

    //Create Database
    string cnnStr = @"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" 
                   + DataSourcePath + fileName + "; Jet OLEDB:Engine Type=5";
    var catType = Type.GetTypeFromProgID("ADOX.Catalog");
    object o = Activator.CreateInstance(catType);
    catType.InvokeMember("Create", 
                         BindingFlags.InvokeMethod, 
                         null, 
                         o, 
                         new object[] { cnnStr });

    OleDbConnection cnn = new OleDbConnection(cnnStr);
    cnn.Open();
    var cmd = cnn.CreateCommand();
    cmd.CommandText = "CREATE TABLE TblInfoCompany (fn TEXT , ln TEXT)";
    cmd.ExecuteNonQuery();
}

Now I want to add caption to two fields that when we open the access file,the first field be "FirstName" and second field be "LastName".

How Can I do this?

Thanks.

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Tavousi
  • 14,848
  • 18
  • 51
  • 70
  • Sorry for misunderstanding your question. Try this: http://stackoverflow.com/questions/3521188/ms-access-setting-table-column-caption-or-description-in-ddl – JleruOHeP Aug 21 '12 at 07:14

1 Answers1

1

Caption has no relevance outside of MS Access, however, here are some notes using interop:

 using Microsoft.Office.Interop.Access.Dao;


        DBEngine dbEng = new DBEngine();
        Workspace ws = dbEng.CreateWorkspace("", "admin", "",
            WorkspaceTypeEnum.dbUseJet);
        Database db = ws.OpenDatabase(@"z:\docs\test.accdb", false, false, "");
        TableDef tdf = db.TableDefs["Table1"];
        Field fld = tdf.Fields["Field1"];

        Property prp = fld.CreateProperty("Caption", 10, "My caption");

        fld.Properties.Append(prp);
Fionnuala
  • 90,370
  • 7
  • 114
  • 152
  • Thanks a lot. Can you tell me: What is "10" in line that set the caption property? This is constant or must be variable in different cases? – Tavousi Aug 22 '12 at 12:47
  • 1
    Yes, it is dbText from the DAO data type enumerator (Microsoft.Office.Interop.Access.Dao.DataTypeEnum.dbText). The additional common values are dbBoolean (1) and dbInteger (3) Ref: http://msdn.microsoft.com/en-us/library/office/bb221004(v=office.12).aspx – Fionnuala Aug 22 '12 at 12:55
  • 1
    Note that the above will throw an error is the caption property already exists for the field. – Fionnuala Aug 22 '12 at 12:59