dataadapter對象的功能

來源:魅力女性吧 2.41W
dataadapter對象的功能

DataAdapter對象和Connection對象、Command對象、DataReader對象一起時構成的四個重要對象之一。

DataAdapter對象在數據集(DataSet)和數據源(DataSource)之間連接中起着橋樑的作用。下面就利用DataAdapter對象來對數據源中的數據進行操作,包括添加、修改和刪除操作。

(1)、添加數據

假如有一個Users表,其中包括如下字段:ID,UserName,Password,現在利用DataAdapter對象的Update方法來向數據源中增加一條數據。

.......

using lient

.......

SqlConnection con = new SqlConnection("數據庫連接字符串")

()

SqlDataAdapter da = new SqlDataAdapter("select * from Users",con)

SqlCommandBuilder scb = new SqlCommandBuilder(da)

DataSet ds = new DataSet()

(ds,"user")

DataRow dr = es["user"]ow()

dr["ID"] = �"

dr["UserName"] = "暖楓無敵"

dr["Password"] = "admin"

es["user"](dr)

te(ds,"user")

..........

在數據源中的表中新增加一行,然後給每個字段賦值,將該行添加到表中,最後利用數據適配器(DataAdapter)更新數據集(DataSet)

將數據表的變化,更新到數據源中去。

(2)、修改數據

接着上面的內容,假如想將新增加的Password字段值更改為admin888的話,該如何操作呢步驟如下:

.......

using lient

.......

SqlConnection con = new SqlConnection("數據庫連接字符串")

()

SqlDataAdapter da = new SqlDataAdapter("select * from Users",con)

SqlCommandBuilder scb = new SqlCommandBuilder(da)

DataSet ds = new DataSet()

(ds,"user")

DataRow dr = es["user"]ct("ID=�' ")

dr[0]["Password"] = "admin888"

te(ds,"user")

..........

(3)、刪除數據

如果要刪除上面的一條記錄,代碼如下:

.......

using lient

.......

SqlConnection con = new SqlConnection("數據庫連接字符串")

()

SqlDataAdapter da = new SqlDataAdapter("select * from Users",con)

SqlCommandBuilder scb = new SqlCommandBuilder(da)

DataSet ds = new DataSet()

(ds,"user")

DataRow dr = es["user"]ct("ID=�' ")

dr[0]te()

te(ds,"user")

..........

熱門標籤