在ADO.NET中,能夠用于執行指令操作的不但有有Command對象,還有DataAdapter對象,DataAdapter對象執行查詢的傳回資料将存儲在DataSet對象中。
DataAdapter對象概述
DataAdapter對象是DataSet和資料之間的橋梁,可以建立并初始化資料表對資料源執行SQL指令,與DataSet對象結合,提供DataSet對象存儲資料,可視為DataSet對象的操作核心。
在使用DataAdapter對象時,隻需要設定表示SQL指令和資料庫連接配接的兩個參數,就可以通過Fill方法把查詢結果放置在一個DataSet對象中。DataAdapter對象可用于執行資料庫的指令操作,含有四個不同的操作指令:
(1)SelectCommand:用來選取資料源中的記錄;
(1)InsertCommand:用來向資料源中新插入一條記錄;
(1)UpdateCommand:用來更新資料源中的資料;
(1)DeleteCommand:用來删除資料源中的記錄。
填充DataSet資料集
在填寫DataSet資料集時,需要用到DataAdapter對象的Fill方法來完成,格式如下:
DataAdapter對象.Fill(DataSet對象,映像源表的名稱的字元串);
例一,利用DataAdapter對象的Fill方法填充DataSet資料集的代碼為:
<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空間
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//連接配接字元串
SqlConnection conText = new SqlConnection(conStr);//建立Connection對象
</span> <span style="font-size:18px;"> try
{
conText.Open();//打開資料庫
string sql = "select * from manager";//建立統計語句
SqlDataAdapter da = new SqlDataAdapter(sql, conText);//建立DataAdapter對象
DataSet ds = new DataSet();//建立DataSet對象
da.Fill(ds, "table");//填充DataSet
Console.WriteLine("填充成功!");
}
catch (Exception ex)//建立檢查Exception對象
{
Console.WriteLine(ex.Message.ToString());//輸出錯誤資訊
</span> <span style="font-size:18px;"> }
finally
{
conText.Close();//關閉連接配接
}
Console.ReadLine();
}
}
}
</span>
在運作結果的時候一定要保證你的資料庫連接配接是正常的,否則會出現非正确的結果。
運作結果為:
![](https://img.laitimes.com/img/9ZDMuAjOiMmIsIjOiQnIsIiclRnblN0LclHdpZXYyd2LcBzNvwVZ2x2bzNXak9CX90TQNNkRrFlQKBTSvwFbslmZvwFMwQzLcVmepNHdu9mZvwFVywUNMZTY18CX052bm9CX90zZNVzaU1UdGdVYzpEWaZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39DOzATMyAzMzITNyUDM1EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
更新資料源
DataAdapter對象的Update方法可用來将DataSet中的更改解析回資料源。Update方法将DataSet映像源表中的DataTable對象或DataTable名稱用做參數。DataSet執行個體包含已作出的更改的DataSet,DataTable辨別從中檢索更改的表。
當調用Update方法時,DataAdapter将分析已作出的更改并執行相應的指令(INSERT,UPDATE或DELETE)。當DataAdapter遇到對DataRow更改時,将使用InsertCommand,UpdateCommand或DeleteCommand來處理該更改。
例二,使用DataAdapter對象更新資料源的代碼為:
<span style="font-size:18px;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;//引入命名空間
using System.Data;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string conStr = "server=.;user=sa;pwd=123456;database=CustomerManagement";//連接配接字元串
SqlConnection conText = new SqlConnection(conStr);//建立Connection對象
try
</span> <span style="font-size:18px;">{
conText.Open();//打開資料庫
string sql = "select * from manager";//建立統計語句
SqlDataAdapter da = new SqlDataAdapter(sql, conText);//建立DataAdapter對象
da.UpdateCommand = new SqlCommand("update manager set userPwd='123456'",conText);
DataSet ds = new DataSet();//建立DataSet對象
da.Fill(ds,"manager");//填充DataSet
DataRow dr = ds.Tables["manager"].Rows[0];
dr["userPwd"] = "userPwd";
da.Update(ds,"manager");//更新資料集
Console.WriteLine("更新成功!");
}
catch (Exception ex)//建立檢查Exception對象
{
Console.WriteLine(ex.Message.ToString());//輸出錯誤資訊
}
finally
{
conText.Close();//關閉連接配接
}
Console.ReadLine();
}
}
}</span>
第一次運作的結果出錯
這是因為代碼中對象執行個體引用設定錯誤,由于并沒有名稱叫做uerTable的資料表,是以更新并沒有進行,将代碼中的出現uerTable的地方均改為manager資料表即可。
再次運作的結果為:
CustomerManagement資料庫中的manager資料表更新後的表顯示為:
使用資料擴充卡: DataAdapter對象有一點是我們必須要注意的:DataSet對象是從資料源中檢索到資料在記憶體中的緩沖,過多使用DataSet對象将會非常占用資源,是以在編寫程式時能使用DataReader對象代替,盡量使用DataReader對象。