1. 程式人生 > 實用技巧 >菜鳥學習ado.net(二)

菜鳥學習ado.net(二)

接著上次學習了ado.net資料提供物件部分,來學習使用者操作部分,主要是關於DataSet的操作。

DateSet是ado.net核心,所有對資料的複雜操作都是使用它,DataSet包含一組DataTabel物件,他們表示所操作的資料表,每個DataTable都有兩個子物件包含DataRow和DataColumn表示資料的行和列。

下面是一段程式碼用DataSet實現讀取資料和更新資料

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Data.SqlClient;
  5. usingSystem.Data;
  6. usingSystem.Text;
  7. namespacedataset
  8. {
  9. classProgram
  10. {
  11. ///<summary>
  12. ///dataset學習
  13. ///</summary>
  14. ///天行健部落格
  15. ///<paramname="args"></param>
  16. staticvoidMain(string[]args)
  17. {
  18. ///******************
  19. ///
  20. ///天行健
  21. ///
  22. ///******************
  23. //讀取表中的資料
  24. using(SqlConnectionconn=newSqlConnection(@"datasource=.\sqlexpress;database=Myoffice;userid=sa;password=123"
    ))//連線資料庫
  25. {
  26. stringsql="selectName,Age,PayfromInfor";
  27. SqlDataAdapterthisAdapter=newSqlDataAdapter(sql,conn);//建立sqlDataAdapter物件
  28. DataSetds=newDataSet();//建立要填充資料的DataSet
  29. thisAdapter.Fill(ds,"Infor");//利用thisAdapter物件,在DataSet中建立Infor填充物件,並執行填充
  30. foreach(DataRowdrinds.Tables["Infor"
    ].Rows)//遍歷DataSet物件
  31. {
  32. Console.WriteLine(dr["Name"]+"\t"+dr["Age"]+"\t"+dr["Pay"]);
  33. }
  34. ds.Clear();
  35. Console.WriteLine("Programfinished!");
  36. //更新資料表
  37. Console.WriteLine("Nowwewillshowupdateskills!");
  38. SqlCommandBuilderthisbuilder=newSqlCommandBuilder(thisAdapter);//建立CommBuilder物件,建立sql命令;負責生成更新資料的sql語句,而不要自己建立這個語句
  39. DataSetthisDataSet=newDataSet();//建立要填充的資料物件DataSet
  40. thisAdapter.Fill(thisDataSet,"table");//填充dataset物件
  41. Console.WriteLine("Namebeforechange:{0}",thisDataSet.Tables["table"].Rows[3]["Name"]);
  42. thisDataSet.Tables["table"].Rows[3]["Name"]="Hen";//更改資料
  43. thisAdapter.Update(ds,"Infor");//執行更新操作
  44. ///SqlDataAdapter.Update這個方法遍歷DataTabe中的行,以找到需要對資料庫做出的變動。
  45. ///Rows集合的每個DataRow物件都具有屬性RowState,可以跟蹤此行是否已刪除、新增、修改,還是沒有變動。反饋到資料庫
  46. Console.WriteLine("Nameafterchange:{0}",thisDataSet.Tables["table"].Rows[3]["Name"]);
  47. thisDataSet.Clear();
  48. Console.WriteLine("Updatafinished!");
  49. Console.ReadLine();
  50. }
  51. Console.ReadKey();
  52. }
  53. }
  54. }
執行結果如下:

下面是執行結果

執行了DataSet的基本操作,下面是用DataSet執行對行的操作

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Text;
  5. usingSystem.Data.SqlClient;
  6. usingSystem.Data;
  7. namespacedataset2
  8. {
  9. classProgram
  10. {
  11. staticvoidMain(string[]args)
  12. {
  13. ///****************************
  14. ///
  15. ///天行健
  16. ///
  17. ///*****************************
  18. using(SqlConnectionconn=newSqlConnection(@"datasource=.\sqlexpress;database=Myoffice;userid=sa;password=123"))
  19. {
  20. //新增行操作
  21. SqlDataAdapterthisAdapter=newSqlDataAdapter("Select*fromInfor",conn);
  22. SqlCommandBuilderthisBuilder=newSqlCommandBuilder(thisAdapter);
  23. DataSetthisdataSet=newDataSet();
  24. thisAdapter.Fill(thisdataSet,"Table");
  25. Console.WriteLine("Rowsafterchange:{0}",thisdataSet.Tables["Table"].Rows.Count);
  26. DataRowthisRow=thisdataSet.Tables["Table"].NewRow();//建立新行物件
  27. thisRow["Id"]=System.Guid.NewGuid();//獲取guid演算法值
  28. thisRow["Name"]="Gerr";
  29. thisRow["Age"]=27;
  30. thisRow["Pay"]=1200;
  31. thisdataSet.Tables["Table"].Rows.Add(thisRow);
  32. Console.WriteLine("Rowsafterchange:{0}",thisdataSet.Tables["Table"].Rows.Count);
  33. thisAdapter.Update(thisdataSet,"Table");
  34. ///dataset只是在記憶體中的資料,dataAdapter負責連線到磁碟上的資料庫中,需要呼叫Update方法才能同步到資料庫
  35. thisdataSet.Clear();
  36. Console.WriteLine("Finished!");
  37. //查詢行操作
  38. SqlDataAdapterSelectAdapter=newSqlDataAdapter("Select*fromInfor",conn);
  39. SqlCommandBuilderSelectBuilt=newSqlCommandBuilder(SelectAdapter);
  40. DataSetSelectDateSet=newDataSet();
  41. thisAdapter.Fill(SelectDateSet,"Table");
  42. //使用find之前,構建主鍵
  43. DataColumn[]keys=newDataColumn[1];
  44. keys[0]=SelectDateSet.Tables["Table"].Columns["Id"];
  45. SelectDateSet.Tables["Table"].PrimaryKey=keys;
  46. DataRowdr=SelectDateSet.Tables["Table"].Rows.Find("add4d8a6-0c99-4ad7-9628-2a6e548a6d73");
  47. if(dr!=null)
  48. {
  49. Console.WriteLine("Find!");
  50. //刪除行操作
  51. Console.WriteLine("Removing.....");
  52. dr.Delete();
  53. SelectAdapter.Update(SelectDateSet,"Table");
  54. Console.WriteLine("Removed!");
  55. }
  56. else
  57. {
  58. Console.WriteLine("NotFind");
  59. }
  60. SelectDateSet.Clear();
  61. Console.WriteLine("Finished!");
  62. Console.ReadLine();
  63. }
  64. }
  65. }
  66. }

執行結果:

一些基本講解都在原始碼裡,這些都是比較簡單的東西,僅供菜鳥學習交流啊。呵呵。。。

下一次是關於ado.net多表的操作和關於讀寫xml內容。。。。。

轉載於:https://blog.51cto.com/1143314007/475742