天天看點

擷取Access資料庫字段的所有屬性(轉)

< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd>

由于操作Access資料庫,需要用增加字段,但又不知道字段的屬性怎麼設定,好不容易找到了一些資料,接湊着把字段屬性找到了。

然後又找了一些資料,用一個最笨的辦法把字段的所有屬性加上。當然,我一直不明白,為什麼微軟不把這些屬性寫出來,而是用了一個集合,非常不明白。

首先自己建立一個access資料庫,建表,建字段,你想設定字段的什麼屬性,自己設定好。

然後用代碼讀這個資料庫,然後列舉它的屬性,需要引用ado 和 adox

目前列舉的屬性有:

0 Autoincrement

1 Default

2 Description

3 Nullable

4 Fixed Length

5 Seed

6 Increment

7 Jet OLEDB:Column Validation Text

8 Jet OLEDB:Column Validation Rule

9 Jet OLEDB:IISAM Not Last Column

10 Jet OLEDB:AutoGenerate

11 Jet OLEDB:One BLOB per Page

12 Jet OLEDB:Compressed UNICODE Strings

13 Jet OLEDB:Allow Zero Length

14 Jet OLEDB:Hyperlink

代碼如下:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using ADOX;

using ADODB;

namespace EnumColumnProperties

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)

            ADODB.ConnectionClass conn = new ADODB.ConnectionClass ();

            string ConnectionString = @"Provider=Microsoft.Jet.Oledb.4.0;Data Source=D:\FirstCatalog.mdb;Persist Security Info=False";

            conn.Open(ConnectionString,"","",0);

            ADOX.CatalogClass catalog = new ADOX.CatalogClass();

            catalog.ActiveConnection = conn;

            foreach (Column co in catalog.Tables[0].Columns )

            {

                foreach (ADODB.Property pr in co.Properties)

                {

                    Console.WriteLine(pr.Name);

                }

                Console.WriteLine(co.Name + "==================");

            }

    }

}

本文轉自 netcorner 部落格園部落格,原文連結: http://www.cnblogs.com/netcorner/archive/2009/06/01/2912066.html  ,如需轉載請自行聯系原作者