菜鳥學習ado.net(二)
阿新 • • 發佈:2020-10-13
接著上次學習了ado.net資料提供物件部分,來學習使用者操作部分,主要是關於DataSet的操作。
DateSet是ado.net核心,所有對資料的複雜操作都是使用它,DataSet包含一組DataTabel物件,他們表示所操作的資料表,每個DataTable都有兩個子物件包含DataRow和DataColumn表示資料的行和列。
下面是一段程式碼用DataSet實現讀取資料和更新資料
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Data.SqlClient;
- usingSystem.Data;
- usingSystem.Text;
- namespacedataset
- {
- classProgram
- {
- ///<summary>
- ///dataset學習
- ///</summary>
- ///天行健部落格
- ///<paramname="args"></param>
- staticvoidMain(string[]args)
- {
- ///******************
- ///
- ///天行健
- ///
- ///******************
- //讀取表中的資料
- using(SqlConnectionconn=newSqlConnection(@"datasource=.\sqlexpress;database=Myoffice;userid=sa;password=123"
- {
- stringsql="selectName,Age,PayfromInfor";
- SqlDataAdapterthisAdapter=newSqlDataAdapter(sql,conn);//建立sqlDataAdapter物件
- DataSetds=newDataSet();//建立要填充資料的DataSet
- thisAdapter.Fill(ds,"Infor");//利用thisAdapter物件,在DataSet中建立Infor填充物件,並執行填充
- foreach(DataRowdrinds.Tables["Infor"
- {
- Console.WriteLine(dr["Name"]+"\t"+dr["Age"]+"\t"+dr["Pay"]);
- }
- ds.Clear();
- Console.WriteLine("Programfinished!");
- //更新資料表
- Console.WriteLine("Nowwewillshowupdateskills!");
- SqlCommandBuilderthisbuilder=newSqlCommandBuilder(thisAdapter);//建立CommBuilder物件,建立sql命令;負責生成更新資料的sql語句,而不要自己建立這個語句
- DataSetthisDataSet=newDataSet();//建立要填充的資料物件DataSet
- thisAdapter.Fill(thisDataSet,"table");//填充dataset物件
- Console.WriteLine("Namebeforechange:{0}",thisDataSet.Tables["table"].Rows[3]["Name"]);
- thisDataSet.Tables["table"].Rows[3]["Name"]="Hen";//更改資料
- thisAdapter.Update(ds,"Infor");//執行更新操作
- ///SqlDataAdapter.Update這個方法遍歷DataTabe中的行,以找到需要對資料庫做出的變動。
- ///Rows集合的每個DataRow物件都具有屬性RowState,可以跟蹤此行是否已刪除、新增、修改,還是沒有變動。反饋到資料庫
- Console.WriteLine("Nameafterchange:{0}",thisDataSet.Tables["table"].Rows[3]["Name"]);
- thisDataSet.Clear();
- Console.WriteLine("Updatafinished!");
- Console.ReadLine();
- }
- Console.ReadKey();
- }
- }
- }
下面是執行結果
執行了DataSet的基本操作,下面是用DataSet執行對行的操作
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Data.SqlClient;
- usingSystem.Data;
- namespacedataset2
- {
- classProgram
- {
- staticvoidMain(string[]args)
- {
- ///****************************
- ///
- ///天行健
- ///
- ///*****************************
- using(SqlConnectionconn=newSqlConnection(@"datasource=.\sqlexpress;database=Myoffice;userid=sa;password=123"))
- {
- //新增行操作
- SqlDataAdapterthisAdapter=newSqlDataAdapter("Select*fromInfor",conn);
- SqlCommandBuilderthisBuilder=newSqlCommandBuilder(thisAdapter);
- DataSetthisdataSet=newDataSet();
- thisAdapter.Fill(thisdataSet,"Table");
- Console.WriteLine("Rowsafterchange:{0}",thisdataSet.Tables["Table"].Rows.Count);
- DataRowthisRow=thisdataSet.Tables["Table"].NewRow();//建立新行物件
- thisRow["Id"]=System.Guid.NewGuid();//獲取guid演算法值
- thisRow["Name"]="Gerr";
- thisRow["Age"]=27;
- thisRow["Pay"]=1200;
- thisdataSet.Tables["Table"].Rows.Add(thisRow);
- Console.WriteLine("Rowsafterchange:{0}",thisdataSet.Tables["Table"].Rows.Count);
- thisAdapter.Update(thisdataSet,"Table");
- ///dataset只是在記憶體中的資料,dataAdapter負責連線到磁碟上的資料庫中,需要呼叫Update方法才能同步到資料庫
- thisdataSet.Clear();
- Console.WriteLine("Finished!");
- //查詢行操作
- SqlDataAdapterSelectAdapter=newSqlDataAdapter("Select*fromInfor",conn);
- SqlCommandBuilderSelectBuilt=newSqlCommandBuilder(SelectAdapter);
- DataSetSelectDateSet=newDataSet();
- thisAdapter.Fill(SelectDateSet,"Table");
- //使用find之前,構建主鍵
- DataColumn[]keys=newDataColumn[1];
- keys[0]=SelectDateSet.Tables["Table"].Columns["Id"];
- SelectDateSet.Tables["Table"].PrimaryKey=keys;
- DataRowdr=SelectDateSet.Tables["Table"].Rows.Find("add4d8a6-0c99-4ad7-9628-2a6e548a6d73");
- if(dr!=null)
- {
- Console.WriteLine("Find!");
- //刪除行操作
- Console.WriteLine("Removing.....");
- dr.Delete();
- SelectAdapter.Update(SelectDateSet,"Table");
- Console.WriteLine("Removed!");
- }
- else
- {
- Console.WriteLine("NotFind");
- }
- SelectDateSet.Clear();
- Console.WriteLine("Finished!");
- Console.ReadLine();
- }
- }
- }
- }
執行結果:
一些基本講解都在原始碼裡,這些都是比較簡單的東西,僅供菜鳥學習交流啊。呵呵。。。
下一次是關於ado.net多表的操作和關於讀寫xml內容。。。。。
轉載於:https://blog.51cto.com/1143314007/475742