欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#使用ADO.Net部件來(lái)訪(fǎng)問(wèn)Access數(shù)據(jù)庫(kù)的方法

 更新時(shí)間:2015年09月07日 13:53:28   投稿:lijiao  
數(shù)據(jù)庫(kù)的訪(fǎng)問(wèn)是所有編程語(yǔ)言中最重要的部分,C#提供了ADO.Net部件用于對(duì)數(shù)據(jù)庫(kù)進(jìn)行訪(fǎng)問(wèn)。本文從最簡(jiǎn)單易用的微軟Access數(shù)據(jù)庫(kù)入手討論在C#中對(duì)數(shù)據(jù)庫(kù)的訪(fǎng)問(wèn)。

數(shù)據(jù)庫(kù)的訪(fǎng)問(wèn)是所有編程語(yǔ)言中最重要的部分,C#提供了ADO.Net部件用于對(duì)數(shù)據(jù)庫(kù)進(jìn)行訪(fǎng)問(wèn)。我們將從最簡(jiǎn)單易用的微軟Access數(shù)據(jù)庫(kù)入手討論在C#中對(duì)數(shù)據(jù)庫(kù)的訪(fǎng)問(wèn)。

C#中的Connection對(duì)象和Command對(duì)象與Access類(lèi)似,但在這里我們還將使用其另一個(gè)與RecordSet類(lèi)似的被稱(chēng)作ADODataReader的對(duì)象,它負(fù)責(zé)處理與查詢(xún)有關(guān)的RecordSet對(duì)象。

首先,必須使用微軟的Access創(chuàng)建一個(gè)數(shù)據(jù)庫(kù)。運(yùn)行Access,創(chuàng)建一個(gè)數(shù)據(jù)庫(kù),但不要?jiǎng)?chuàng)建任何表(我們將在下面的程序中創(chuàng)建表。),保存創(chuàng)建的數(shù)據(jù)庫(kù)。

打開(kāi)控制面板中的ODBC圖標(biāo),點(diǎn)擊System DNS標(biāo)簽,依次選擇Add>Microsoft Access,并點(diǎn)擊Finish按鈕。在拉下來(lái)的對(duì)話(huà)框中輸入數(shù)據(jù)源的名字,比如是mymdb,然后創(chuàng)建數(shù)據(jù)源,雙擊OK按鈕。

在下面的程序中,我們將創(chuàng)建一個(gè)表,并在其中插入一些值。

程序非常地簡(jiǎn)單和直觀。在Main()函數(shù)中,ADOConnection對(duì)象將數(shù)據(jù)源的名字取到構(gòu)造器中,然后使用ADOConenction的Open()方法打開(kāi)該連接。

在連接建立后,程序?qū)?chuàng)建包含二個(gè)字段的表a1,其中字段name的類(lèi)型為字符型,vno的類(lèi)型為整型。Create table命令已經(jīng)放在ADOCommand的構(gòu)造器中,ExecuteNonQuery()方法用于執(zhí)行這一查詢(xún),該方法不會(huì)返回任何記錄集。同樣,Insert和Delete查詢(xún)也可以放到ADOCommand的Constructor中,因此可以象在VB中那樣傳遞任何SQL查詢(xún)。

ADODataReader是新出現(xiàn)的,它是本段程序中主要的對(duì)象,負(fù)責(zé)處理ADOCommand返回的記錄集。使用xecute()方法,就可以看到從數(shù)據(jù)庫(kù)中返回的數(shù)據(jù)。ADODataReader的Read()方法則返回布爾型的值,TRUE標(biāo)志著數(shù)據(jù)在ADODataReader對(duì)象中,而且將當(dāng)前指針移動(dòng)到了ADODataReader對(duì)象的下一條記錄上。

使用Visual Studio.Net 編譯下面的程序代碼。 

 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"); 
  
 //創(chuàng)建表 
 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); 
  
 InsTable.ExecuteNonQuery(); 
 Console.WriteLine("Values Inserted"); 
 } 
  
 //刪除整個(gè)表 
 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(); 
 } 
  
 s.Close(); 
 Console.ReadLine(); 
 } 
 catch(System.Exception e) 
 { 
 Console.WriteLine(e.ToString()); 
 Console.ReadLine(); 
 } 
  
 return 0; 
 } // Main函數(shù)結(jié)束 
 } // Class結(jié)束 
 }// 名字空間結(jié)束

以上就是 在C#中使用ADO.Net部件來(lái)訪(fǎng)問(wèn)Access數(shù)據(jù)庫(kù)的過(guò)程,希望對(duì)大家的學(xué)習(xí)有所幫助。

相關(guān)文章

  • Unity實(shí)現(xiàn)鼠標(biāo)點(diǎn)2D轉(zhuǎn)3D進(jìn)行旋轉(zhuǎn)

    Unity實(shí)現(xiàn)鼠標(biāo)點(diǎn)2D轉(zhuǎn)3D進(jìn)行旋轉(zhuǎn)

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)鼠標(biāo)點(diǎn)2D轉(zhuǎn)3D進(jìn)行旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 最新評(píng)論