C#使用ADO.Net部件来访问Access数据库的方法

2019-12-26 13:13:01刘景俊
  • string ch = Console.ReadLine();   if (ch == "y")  
  • {   ADOCommand CreateTable = new ADOCommand("Create Table a1(vno integer,name char(20))", s);  
  • CreateTable.ExecuteNonQuery();   Console.WriteLine("AOCommand Executed / Table Created");  
  • }    
  • //在表中插入值   Console.Write("Want to Insert Some values in a Table?(y/n) ");  
  • ch = Console.ReadLine();   if (ch == "y")  
  • {   ADOCommand InsTable = new 
  • ADOCommand("insert into a1 values(1, 'hi')", s);    
  • InsTable.ExecuteNonQuery();   Console.WriteLine("Values Inserted");  
  • }    
  • //删除整个表   Console.Write("Want to Delete All Records Present in the Table?(y/n) ");  
  • ch = Console.ReadLine();   if (ch == "y")  
  • {   ADOCommand DeleteTable = new ADOCommand("Delete from a1", s);  
  • DeleteTable.ExecuteNonQuery();   Console.WriteLine("All Records Deleted From the Table");  
  • }    
  • //看所有记录   Console.Write("Want to See all the Records Present in the Table /Database (y/n)? ");  
  • ch = Console.ReadLine();   if (ch == "y")  
  • {   ADOCommand AllRecs = new ADOCommand("select * from a1", s);  
  • ADODataReader r;   AllRecs.Execute(out r);  
  • while(r.Read())   {  
  • for(int i=0; i < r.FieldCount;i++)   {  
  • Console.Write(r.GetValue(i)+ " ");   }  
  • Console.WriteLine();   }  
  • Console.WriteLine("All Records Displayed");   r.Close();