namespace database1 { using System; using System.Data.ADO;
public class Class1 { public Class1() { // // 在这里添加Constructor的逻辑 // }
public static int Main(string[] args) { try { ADOConnection s = new ADOConnection("Data Source=mymdb"); s.Open(); Console.WriteLine("Connection Established");
//创建表 Console.Write("Want to Create a Table?(y/n) "); 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);
//删除整个表 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(); }