View Code
1 //获取Access数据库表的字段名 2 public List<string> GetTableFieldNameList(string TableName) 3 { 4 string conStr = "provider=microsoft.jet.oledb.4.0;"; 5 conStr += @"data source=" + 数据库的路径(例如:"D:\\data\\test.mdb") 6 List<string> list = new List<string>(); 7 OleDbConnection con = new OleDbConnection(conStr); 8 try 9 { 10 con.Open(); 11 using (OleDbCommand cmd = new OleDbCommand()) 12 { 13 cmd.CommandText = "SELECT TOP 1 * FROM [" + TableName + "]"; 14 cmd.Connection = con; 15 OleDbDataReader dr = cmd.ExecuteReader(); 16 for (int i = 0; i < dr.FieldCount; i++) 17 { 18 list.Add(dr.GetName(i)); 19 } 20 } 21 return list; 22 } 23 catch (Exception e) 24 { throw e; } 25 finally 26 { 27 if (con.State == ConnectionState.Open) 28 con.Close(); 29 con.Dispose(); 30 } 31 }
View Code
1 //获取Access数据库表名集合 2 public ArrayList GetTableNameList(string filePath) 3 { 4 string conStr = "provider=microsoft.jet.oledb.4.0;"; 5 conStr += @"data source=" + filePath; 6 ArrayList tableList = new ArrayList(); 7 OleDbConnection con = new OleDbConnection(conStr); 8 try 9 { 10 con.Open(); 11 //第一种方法 12 /*DataRow [] rows = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null).Select(); 13 for (int i = 0; i < rows.Length; i++) 14 { 15 if (!rows[i]["TABLE_NAME"].ToString().Contains("MSys")) 16 { 17 tableList.Add(rows[i]["TABLE_NAME"].ToString()); 18 } 19 } 20 return tableList;*/ 21 //第二种方法 22 DataTable dt = con.GetSchema("Tables"); 23 foreach (DataRow row in dt.Rows) 24 { 25 if (row[3].ToString() == "TABLE") 26 tableList.Add(row[2].ToString()); 27 } 28 return tableList; 29 } 30 catch (Exception e) 31 { 32 throw e; 33 } 34 finally 35 { 36 if (con.State == ConnectionState.Open) 37 con.Close(); 38 con.Dispose(); 39 } 40 }
添加Access数据库的的字段名
alter table tableName add column 字段名 int