C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
數(shù)據(jù)庫的訪問是所有編程語言中最重要的部分,C#提供了ADO.Net部件用于對數(shù)據(jù)庫進行訪問。我們將從最簡單易用的微軟Access數(shù)據(jù)庫入手討論在C#中對數(shù)據(jù)庫的訪問。
C#中的Connection對象和Command對象與Access類似,但在這里我們還將使用其另一個與RecordSet類似的被稱作ADODataReader的對象,它負責處理與查詢有關(guān)的RecordSet對象。
首先,必須使用微軟的Access創(chuàng)建一個數(shù)據(jù)庫。運行Access,創(chuàng)建一個數(shù)據(jù)庫,但不要創(chuàng)建任何表(我們將在下面的程序中創(chuàng)建表。),保存創(chuàng)建的數(shù)據(jù)庫。
打開控制面板中的ODBC圖標,點擊System DNS標簽,依次選擇Add>Microsoft Access,并點擊Finish按鈕。在拉下來的對話框中輸入數(shù)據(jù)源的名字,比如是mymdb,然后創(chuàng)建數(shù)據(jù)源,雙擊OK按鈕。
在下面的程序中,我們將創(chuàng)建一個表,并在其中插入一些值。
程序非常地簡單和直觀。在Main()函數(shù)中,ADOConnection對象將數(shù)據(jù)源的名字取到構(gòu)造器中,然后使用ADOConenction的Open()方法打開該連接。
在連接建立后,程序?qū)?chuàng)建包含二個字段的表a1,其中字段name的類型為字符型,vno的類型為整型。Create table命令已經(jīng)放在ADOCommand的構(gòu)造器中,ExecuteNonQuery()方法用于執(zhí)行這一查詢,該方法不會返回任何記錄集。同樣,Insert和Delete查詢也可以放到ADOCommand的Constructor中,因此可以象在VB中那樣傳遞任何SQL查詢。
ADODataReader是新出現(xiàn)的,它是本段程序中主要的對象,負責處理ADOCommand返回的記錄集。使用xecute()方法,就可以看到從數(shù)據(jù)庫中返回的數(shù)據(jù)。ADODataReader的Read()方法則返回布爾型的值,TRUE標志著數(shù)據(jù)在ADODataReader對象中,而且將當前指針移動到了ADODataReader對象的下一條記錄上。
使用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"); } //刪除整個表 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部件來訪問Access數(shù)據(jù)庫的過程,希望對大家的學習有所幫助。
相關(guān)文章
c#中的浮點型轉(zhuǎn)整形的舍取 四舍五入和銀行家舍入實現(xiàn)代碼
c#中的浮點型轉(zhuǎn)整形的舍取 四舍五入和銀行家舍入實現(xiàn)代碼,學習c#的朋友可以參考下2012-03-03Win10 系統(tǒng)下VisualStudio2019 配置點云庫 PCL1.11.0的圖文教程
這篇文章主要介紹了Win10 系統(tǒng)下VisualStudio2019 配置點云庫 PCL1.11.0的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07C#實現(xiàn)DataTable轉(zhuǎn)TXT、CSV文件
這篇文章介紹了C#實現(xiàn)DataTable轉(zhuǎn)TXT、CSV文件的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04http圖片上傳安全性問題 根據(jù)ContentType (MIME) 判斷其實不準確、不安全
圖片上傳常用的類型判斷方法有這么幾種---截取擴展名、獲取文件ContentType (MIME) 、讀取byte來判斷(這個什么叫法來著?)。下面由腳本之家小編跟大家分享圖片上傳安全性問題,感興趣的朋友一起看看吧2015-09-09C#利用正則表達式實現(xiàn)獲取字符串中漢字的數(shù)量
這篇文章主要為大家詳細介紹了C#如何利用正則表達式實現(xiàn)獲取字符串中漢字的數(shù)量,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下2024-01-01

Unity實現(xiàn)鼠標點2D轉(zhuǎn)3D進行旋轉(zhuǎn)