ADO內(nèi)存數(shù)據(jù)對象SqlDataAdapter類使用SqlDataAdapter在數(shù)據(jù)庫和DataSet之間架起橋梁:
void?UseDataAdapter() ?{ ?//1?連接數(shù)據(jù)庫 ??//2?創(chuàng)建一個SqlDataAdapter對象 ?SqlDataAdapter?adapter?=?new?SqlDataAdapter(); ??//3?利用SqlDataAdapter查詢數(shù)據(jù),并把數(shù)據(jù)放在DataSet中 ?SqlCommand?selectCmd?=?new?SqlCommand(); ?selectCmd.Connection?=?myCon; ?selectCmd.CommandText?=?"SELECT?SupplierID,?CompanyName?FROM?Suppliers;"; ?adapter.SelectCommand?=?selectCmd; ??DataSet?dataSet?=?new?DataSet("Suppliers"); ?adapter.Fill(dataSet);//Fill數(shù)據(jù) ?Page.Response.Write("已連查詢數(shù)據(jù),并放入DataSet中<br?/>"); ??//4?利用SqlDataAdapter對象操作語句 ?//4.1?Update ?SqlCommand?updateCmd?=?new?SqlCommand(); ?updateCmd.Connection?=?myCon; ?updateCmd.CommandText?=?"UPDATE?Customers?SET?CompanyName='KFC'?WHERE?CustomerID?=?'ALFKI'"; ?adapter.UpdateCommand?=?updateCmd; ?//4.2?Delete ?SqlCommand?deleteCmd?=?new?SqlCommand(); ?deleteCmd.Connection?=?myCon; ?deleteCmd.CommandText?=?"DELETE?Customers?WHERE?CustomerID?=?'ALFKI'"; ?adapter.DeleteCommand=?deleteCmd; ?//4.3?Insert ?SqlCommand?insertCmd?=?new?SqlCommand(); ?insertCmd.Connection?=?myCon; ?insertCmd.CommandText?=?"INSERT?INTO?Customers(CustomerId)?values?('ALFKI')"; ?adapter.InsertCommand?=?insertCmd; ?//4.4?執(zhí)行操作 ?adapter.Update(dataSet); ?Page.Response.Write("已連操作數(shù)據(jù),包括:修改、刪除、插入<br?/>"); ??//5?關(guān)閉數(shù)據(jù)庫連接 ?myCon.Close(); ?Page.Response.Write("已關(guān)閉數(shù)據(jù)庫<br?/>"); ?} ?
總結(jié)
以上是生活随笔為你收集整理的ADO内存数据对象SqlDataAdapter类使用SqlDataAdapter在数据库和DataSet之间架起桥梁:的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。