ADO.NET簡單知識整理

來源:互聯網
上載者:User

ADO.NET
Consumer objects

Dataset:資料存在記憶體中,DataSet 是一個資料集,包括
DataTable和 DataRelation,而DataTable 中又包含DataRow和DataColumn,當資料讀取至DataSet中後,就可以以索引或者表名的方式對DataTable進行CRUD操作。

.NET
Data Provider objects

Connection:資料庫連接對象。

作用:1、串連資料庫;2、建立Command 對象

比如:

SqlConnection thisConnection = new SqlConnection(@"server=.\sqlexpress;Integrated Security =
true;Database = C:\SQL SERVER 2000 SAMPLE DATABASES\NORTHWND.MDF");

thisConnection.Open();

SqlCommand thisCommand =
thisConnection.CreateCommand();

Command:資料庫命令對象,命令類型可以是SQL語句,也可以是預存程序。

比如:

thisCommand.CommandType = CommandType.StoredProcedure;

thisCommand.CommandText = "Ten
Most Expensive Products";

CommandBuilder:用於構建SQL命令,常常與DataAdapter對象組合使用,可自動產生SQL語句。

比如:

SqlDataAdapter thisAdapter = new
SqlDataAdapter("select
CustomerID from Customers", thisConnection);

SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);

可以擷取CommandBuilder對象中自動產生的CRUD方法。

DataReader:用於讀取僅能前向和唯讀資料量,對於簡單的讀取資料時,該對象效能最好。

比如:

SqlCommand thisCommand =
thisConnection.CreateCommand();

thisCommand.CommandText = "select
CustomerID, CompanyName from Customers";

SqlDataReader thisReader =
thisCommand.ExecuteReader();

DataAdapter:資料庫和記憶體中的橋樑,讀取資料庫中的資料來填充DataSet 對象,Fill()方法就是他的。

DataSet thisDataSet = new
DataSet();

SqlDataAdapter custAdapter = new
SqlDataAdapter("select
* from Customers", thisConnection);

custAdapter.Fill(thisDataSet, "Customers");

資料填充至DataSet對象後,便可對其中的某張表進行操作,對某張表的每一行和每一列均可進行CRUD操作。

 

ADO.NET和XML

可以將DataSet對象的資料寫入XML檔案:

比如:

DataSet thisDataSet = new
DataSet();

orderAdapter.Fill(thisDataSet, "Orders");

thisDataSet.WriteXml(@"d:\tmp\nwinddata.xml");

當然,也可以從XML檔案讀取資料至DataSet對象:

比如:

DataSet thisDataSet = new
DataSet();

thisDataSet.ReadXml(@"d:\tmp\nwinddata.xml");

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.