How can I use C # Or VB. NET to read and write fields such as the title and Description of the Access database?

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.