在项目中用到了导ACCESS表数据及结构。于是有了下文:
Code
1 /**//// <summary>
2 /// 将原库所有表数据导入目标库中所有同名表中
3 /// </summary>
4 /// <param name="DesFileName">目标库文件</param>
5 /// <param name="ResFileName">原库文件</param>
6 private static void ExpoData(string DesFileName, string ResFileName)
7 {
8 DBA db = new DBA(DesFileName, false);
9 using (db)
10 {
11 //get all table name
12 string ConnectionString = @"Provider=Microsoft.Jet.OleDB.4.0;Data Source=" + ResFileName;
13 OleDbConnection conn = new OleDbConnection(ConnectionString);
14 conn.Open();
15 DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
16 new object[] { null, null, null, "TABLE" });
17 foreach (DataRow dr in schemaTable.Rows)
18 {
19 //表名
20 MessageBox.Show(dr["TABLE_NAME"].ToString());
21
22 //字段名
23 DataTable columnTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
24 new object[] { null, null, dr["TABLE_NAME"].ToString(), null });
25
26 foreach (DataRow dr2 in columnTable.Rows)
27 {
28 MessageBox.Show("{0}", dr2["COLUMN_NAME"].ToString());
29 }
30
31 }
32 conn.Close();
33
34 if (0 == db.ExecuteNonQuery("select * into " + DesFileName + " from " + ResFileName))
35 MessageBox.Show("Export data successful");
36 else
37 MessageBox.Show("Export data failed");
38 }
39 }