How can I use C # Or VB. NET to read and write the title information of the Access database? Some information of the Access database cannot be read through ADO. NET. DAO must be used for read/write. These attributes include:
Caption
ColumnHidden
ColumnOrder
ColumnWidth
DecimalPlaces
Description
Format
InputMask
Wait, you can use the following method to read and write DAO, with the complete source code attached:
C # code:
/// You need to add a reference to the Microsoft DAO 3.6 Object Library
/// Pass the test in VS 2008 and Access 2003
Const int dbUseJet = 2;
Const int dbPropNotFound = 3270;
Const dao. DataTypeEnum dbText = dao. DataTypeEnum. dbText;
String dbName = @ "E: WebSite1MengXianhuidb3. mdb ";
Dao. Workspace DAOWorkspace;
Dao. Database DAODatabase;
Dao. DBEngine DAODBEngine = new dao. DBEngine ();
// Create a workspace
DAOWorkspace = DAODBEngine. CreateWorkspace ("WorkSpace", "Admin", "", dbUseJet );
// Open the database
DAODatabase = DAOWorkspace. OpenDatabase (dbName, false, false, null );
Dao. TableDef DAOTable;
Dao. Field DAOField;
// Table object
DAOTable = DAODatabase. TableDefs ["Table1"];
DAOField = DAOTable. Fields ["BirthDay"];
// Read the "title" attribute of the UserName field. If the title is not set, an exception is thrown.
// If the title does not exist, add a title.
String CaptionText;
Try
{
CaptionText = DAOField. Properties ["Caption"]. Value. ToString ();
}
Catch
{
If (dbPropNotFound = DAODBEngine. Errors [0]. Number)
{
// If the property does not exist, add an attribute.
Dao. Property dbProperty = DAOField. CreateProperty ("Caption", dbText, "birthdate field title", false );
DAOField. Properties. Append (dbProperty );
DAOField. Properties. Refresh ();
CaptionText = DAOField. Properties ["Caption"]. Value. ToString ();
}
Else
{
CaptionText = "this title is not available and a title cannot be created. ";
}
}
Finally
{
DAODatabase. Close ();
System. Runtime. InteropServices. Marshal. ReleaseComObject (DAOField );
System. Runtime. InteropServ