1 //连接Access 2007数据库的方法和以前连接Access 2003的类似。 2 3 using System.Data; 4 using System.Data.OleDb; 5 // 6 public class Acc 7 { 8 //Microsoft.ACE.OLEDB.12.0是Access 2007的数据库提供程序; 9 //sdu.accdb是Microsoft Access 2007数据库文件;accdb是其扩展名。 10 private static string Cons = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\\CS\\sdu.accdb"; 11 public static void Main() 12 { 13 using(OleDbConnection Con = new OleDbConnection(Cons)) 14 { 15 OleDbCommand Cmd = new OleDbCommand("SELECT * FROM tab1",Con); 16 OleDbDataReader Rea = null; 17 try 18 { 19 Con.Open(); 20 Rea = Cmd.ExecuteReader(); 21 while (Rea.Read()) 22 { 23 System.Console.WriteLine("\t{0}\t{1}\t{2}",Rea[0],Rea[1],Rea[2]); 24 } 25 } 26 catch (System.Exception ex) 27 { 28 System.Console.WriteLine(ex); 29 } 30 finally 31 { 32 Rea.Close(); 33 } 34 } 35 } 36 }