可以使用Connection或DataReader对象的GetSchemaTable方法获取数据库的架构信息。
1using System;
2using System.Drawing;
3using System.Collections;
4using System.ComponentModel;
5using System.Windows.Forms;
6using System.Data;
7using System.Data.OleDb;
8
9namespace GetDataBaseSchema
10{
11 /**//// <summary>
12 /// frmGetDBSchema 的摘要说明。
13 /// </summary>
14 public class frmGetDBSchema : System.Windows.Forms.Form
15 {
16 public frmGetDBSchema()
17 {
18 //
19 // Windows 窗体设计器支持所必需的
20 //
21 InitializeComponent();
22
23 //
24 // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
25 //
26 }
27
28 /**//// <summary>
29 /// 清理所有正在使用的资源。
30 /// </summary>
31 protected override void Dispose( bool disposing )
32 {
33 if( disposing )
34 {
35 if (components != null)
36 {
37 components.Dispose();
38 }
39 }
40 base.Dispose( disposing );
41 }
42
43 Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
44
45 private System.Windows.Forms.Button btnChooseDBFile;
46 private System.Windows.Forms.Label label1;
47 private System.Windows.Forms.TextBox txtFileName;
48 private System.Windows.Forms.ComboBox cboTables;
49 private System.Windows.Forms.DataGrid dataGrid1;
50 private System.Windows.Forms.OpenFileDialog openFileDialog1;
51 /**//// <summary>
52 /// 必需的设计器变量。
53 /// </summary>
54 private System.ComponentModel.Container components = null;
55
56 /**//// <summary>
57 /// 设计器支持所需的方法 - 不要使用代码编辑器修改
58 /// 此方法的内容。
59 /// </summary>
60 private void InitializeComponent()
61 {
62 this.btnChooseDBFile = new System.Windows.Forms.Button();
63 this.label1 = new System.Windows.Forms.Label();
64 this.txtFileName = new System.Windows.Forms.TextBox();
65 this.cboTables = new System.Windows.Forms.ComboBox();
66 this.dataGrid1 = new System.Windows.Forms.DataGrid();
67 this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
68 ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
69 this.SuspendLayout();
70 //
71 // btnChooseDBFile
72 //
73 this.btnChooseDBFile.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
74 this.btnChooseDBFile.Location = new System.Drawing.Point(20, 24);
75 this.btnChooseDBFile.Name = "btnChooseDBFile";
76 this.btnChooseDBFile.Size = new System.Drawing.Size(104, 28);
77 this.btnChooseDBFile.TabIndex = 0;
78 this.btnChooseDBFile.Text = "选择数据库文件";
79 this.btnChooseDBFile.Click += new System.EventHandler(this.btnChooseDBFile_Click);
80 //
81 // label1
82 //
83 this.label1.Location = new System.Drawing.Point(24, 64);
84 this.label1.Name = "label1";
85 this.label1.TabIndex = 1;
86 this.label1.Text = "选 择 表:";
87 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
88 //
89 // txtFileName
90 //
91 this.txtFileName.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
92 this.txtFileName.Location = new System.Drawing.Point(140, 28);
93 this.txtFileName.Name = "txtFileName";
94 this.txtFileName.ReadOnly = true;
95 this.txtFileName.Size = new System.Drawing.Size(272, 21);
96 this.txtFileName.TabIndex = 2;
97 this.txtFileName.Text = "";
98 //
99 // cboTables
100 //
101 this.cboTables.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
102 this.cboTables.Location = new System.Drawing.Point(140, 64);
103 this.cboTables.Name = "cboTables";
104 this.cboTables.Size = new System.Drawing.Size(272, 20);
105 this.cboTables.TabIndex = 3;
106 this.cboTables.SelectedIndexChanged += new System.EventHandler(this.cboTables_SelectedIndexChanged);
107 //
108 // dataGrid1
109 //
110 this.dataGrid1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
111 | System.Windows.Forms.AnchorStyles.Left)
112 | System.Windows.Forms.AnchorStyles.Right)));
113 this.dataGrid1.DataMember = "";
114 this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
115 this.dataGrid1.Location = new System.Drawing.Point(8, 96);
116 this.dataGrid1.Name = "dataGrid1";
117 this.dataGrid1.ReadOnly = true;
118 this.dataGrid1.Size = new System.Drawing.Size(416, 224);
119 this.dataGrid1.TabIndex = 4;
120 //
121 // openFileDialog1
122 //
123 this.openFileDialog1.Filter = "Access数据库文件(*.mdb)|*.mdb";
124 this.openFileDialog1.Title = "选择数据库文件";
125 //
126 // frmGetDBSchema
127 //
128 this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
129 this.ClientSize = new System.Drawing.Size(432, 333);
130 this.Controls.Add(this.dataGrid1);
131 this.Controls.Add(this.cboTables);
132 this.Controls.Add(this.txtFileName);
133 this.Controls.Add(this.label1);
134 this.Controls.Add(this.btnChooseDBFile);
135 this.Name = "frmGetDBSchema";
136 this.Text = "获取Access数据库架构信息";
137 ((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
138 this.ResumeLayout(false);
139
140 }
141 #endregion
142
143 /**//// <summary>
144 /// 应用程序的主入口点。
145 /// </summary>
146 [STAThread]
147 static void Main()
148 {
149 Application.Run(new frmGetDBSchema());
150 }
151
152 变量区#region 变量区
153
154 private string conStr;
155 private OleDbConnection conn;
156 private DataTable dt;
157
158 #endregion
159
160 功能区#region 功能区
161
162 /**//// <summary>
163 /// 选择数据库文件
164 /// </summary>
165 private void ChooseDBFile()
166 {
167 if(this.openFileDialog1.ShowDialog() == DialogResult.OK)
168 {
169 this.txtFileName.Text = this.openFileDialog1.FileName;
170 this.conStr = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}",this.openFileDialog1.FileName);
171 conn = new OleDbConnection(conStr);
172 try
173 {
174 Cursor.Current = Cursors.WaitCursor;
175 conn.Open();
176 dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,null);
177 this.dataGrid1.CaptionText = "表信息";
178 this.dataGrid1.DataSource = dt;
179 this.conn.Close();
180 FillTableList();
181 }
182 catch(Exception ex)
183 {
184 MessageBox.Show(ex.Message);
185 }
186 }
187 }
188 /**//// <summary>
189 /// 填充表名列表
190 /// </summary>
191 private void FillTableList()
192 {
193 this.cboTables.Items.Clear();
194 foreach(DataRow dr in dt.Rows)
195 {
196 cboTables.Items.Add(dr["TABLE_NAME"]);
197 }
198 }
199 /**//// <summary>
200 /// 选择某个表,填充其字段
201 /// </summary>
202 private void OnChooseTable()
203 {
204 if(cboTables.SelectedIndex == -1)
205 return;
206 if(conn == null)
207 {
208 MessageBox.Show("请选择数据库文件!");
209 return;
210 }
211 if(conn.State == ConnectionState.Closed)
212 conn.Open();
213 string table = this.cboTables.GetItemText(this.cboTables.SelectedItem);
214 object[] objRes = new object[]{null,null,table,null};
215 dt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,objRes);
216 this.dataGrid1.CaptionText = "字段信息";
217 this.dataGrid1.DataSource = dt;
218 this.conn.Close();
219 }
220
221 #endregion
222
223 private void btnChooseDBFile_Click(object sender, System.EventArgs e)
224 {
225 ChooseDBFile();
226 }
227
228 private void cboTables_SelectedIndexChanged(object sender, System.EventArgs e)
229 {
230 OnChooseTable();
231 }
232 }
233}