1 / 47

ADO.NET

ADO.NET. ADO.NET. 精靈化設計 程式化設計 SQL 指令 DataGrid, DataList DataBind. 精靈化設計. 工具物件 SqlConnection SqlCommand SqlAdapter DataGrid DataSource DataBind 自動化格式 屬性產生器. DataGrid. Step 1: 在版面配置一個 DataGrid. DataGrid. Step 2: 設定資料庫連線. DataGrid. Step 2: 設定資料庫連線. DataGrid. Step 2: 設定資料庫連線.

tryna
Download Presentation

ADO.NET

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ADO.NET

  2. ADO.NET • 精靈化設計 • 程式化設計 • SQL 指令 • DataGrid, DataList • DataBind

  3. 精靈化設計 • 工具物件 • SqlConnection • SqlCommand • SqlAdapter • DataGrid • DataSource • DataBind • 自動化格式 • 屬性產生器

  4. DataGrid Step 1:在版面配置一個DataGrid

  5. DataGrid Step 2:設定資料庫連線

  6. DataGrid Step 2:設定資料庫連線

  7. DataGrid Step 2:設定資料庫連線

  8. DataGrid Step 2:設定資料庫連線

  9. DataGrid Step 2:設定資料庫連線

  10. DataGrid Step 2:設定資料庫連線

  11. DataGrid Step 2:設定資料庫連線

  12. DataGrid Step 2:設定資料庫連線

  13. DataGrid Step 2:設定資料庫連線

  14. DataGrid Step 2:設定資料庫連線

  15. DataGrid Step 3:產生資料集DataSet

  16. DataGrid Step 3:產生資料集DataSet

  17. DataGrid Step 3:產生資料集DataSet

  18. DataGrid Step 4:設定DataGrid的DataSource

  19. DataGrid Step 4:設定DataGrid的DataSource

  20. DataGrid Step 5:設定DataGrid的DataBind

  21. DataGrid

  22. 美化DataGrid

  23. 使用DataGrid進行資料異動

  24. 屬性產生器

  25. 排序 要設定DataGrid的AllowSorting • private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) • { • SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees ORDER BY " + e.SortExpression + " DESC ",sqlConnection1); • da.Fill(ds12,”employees”); • DataGrid1.DataBind(); • } 只能排序一次,使用Hidden控制項改成連續排序

  26. 完整排序 private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e) { SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees ORDER BY " + e.SortExpression + " " + Hidden1.Value.ToString(),sqlConnection1); da.Fill(ds1,”employees”); if(Hidden1.Value=="desc") { Hidden1.Value="asc"; }else{ Hidden1.Value="desc"; } DataGrid1.DataBind(); } 注意IsPostBack

  27. 模組化 showdata • private void showdata() • { • SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees",sqlConnection1); • da.Fill(ds12,” employees”); • DataGrid1.DataBind(); • }

  28. 分頁

  29. 分頁 • private void showdata() • { • SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees",sqlConnection1); • da.Fill(ds12,”employees”); • DataGrid1.DataBind(); • } • private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e) • { • DataGrid1.CurrentPageIndex=e.NewPageIndex; • showdata(); • }

  30. 選取資料 I

  31. 選取資料 I • private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) • { • Response.Write(e.Item.Cells[0].Text); • Response.Write(DataGrid1.DataKeys[e.Item.ItemIndex]); • showdata(); • } 要設定DataKeyField屬性

  32. 選取資料 I (子母表單) • private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) • { • switch(e.CommandName.ToString()) • { • case "Select": • SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM employees where employeeID=" + DataGrid1.DataKeys[e.Item.ItemIndex] ,sqlConnection1); • DataSet ds2=new DataSet(); • da.Fill(ds2,”employees”); • DataGrid2.DataSource=ds2; • DataGrid2.DataBind(); • break; • } • }

  33. 選取資料 II 加入樣板 編輯樣板 要將AutoPostBack設定成True

  34. 選取資料 II 加入樣板

  35. 選取資料 II • 不像DataGrid有預設對應的事件,需要自行設定 • 初始化CheckBox事件 (寫在Page_Load事件中) • for(int i=0;i<=DataGrid1.Items.Count-1;i++) • { • ((CheckBox)DataGrid1.Items[i].Cells[5].Controls[1]).CheckedChanged+= new EventHandler(selecteditem); • } 事件名稱

  36. 選取資料 II • private void selecteditem(object sender, System.EventArgs e) • { • string str; • str="1=0"; • for(int i=0;i<=DataGrid1.Items.Count-1;i++) • { • if(((CheckBox)DataGrid1.Items[i].Cells[5].Controls[1]).Checked) • { • str+=" or employeeID=" + DataGrid1.Items[i].Cells[0].Text; • } • } • SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM Orders where " + str,sqlConnection1); • DataSet ds2=new DataSet(); • da.Fill(ds2,”company”); • DataGrid2.DataSource=ds2; • DataGrid2.DataBind(); • }

  37. 編輯

  38. 編輯 • private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) • { • DataGrid1.EditItemIndex=e.Item.ItemIndex; • showdata(); • } • private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) • { • DataGrid1.EditItemIndex=-1; • showdata(); • }

  39. 編輯 • private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) • { • SqlDataAdapter da=new SqlDataAdapter("update employees set LastName='" + ((TextBox)e.Item.Cells[1].Controls[0]).Text + "' where employeeID='" + DataGrid1.DataKeys[e.Item.ItemIndex] + "'",sqlConnection1); • da.Fill(ds1,”employees”); • DataGrid1.EditItemIndex=-1; • showdata(); • } 注意! PK值不應該可以更改

  40. 新增 SqlDataAdapter da=new SqlDataAdapter("insert into employees (pwd) values ('" + TextBox1.Text + "')",sqlConnection1); da.Fill(dataset12,”employees”); showdata(); 可以配合Panel物件

  41. 刪除

  42. 刪除 • private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e) • { • SqlDataAdapter da=new SqlDataAdapter("delete from employees where id='" + DataGrid1.DataKeys[e.Item.ItemIndex] + "'",sqlConnection1); • da.Fill(dataset12,”employees”); • if(e.Item.ItemIndex==0) //判斷是不是那頁最後一筆 • { • if(DataGrid1.CurrentPageIndex !=0) • { • DataGrid1.CurrentPageIndex=DataGrid1.CurrentPageIndex-1; • } • } • showdata(); • }

  43. 刪除 + Confirm視窗 • private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e) • { • if(e.Item.DataItem==null) return; • string msg="javascript:return confirm('確定要刪除" + DataGrid1.DataKeys[e.Item.ItemIndex].ToString().Trim() + "?')"; • ((Button)e.Item.Cells[4].Controls[0]).Attributes["onclick"]=msg; • } 需將”刪除”選項改成按紐形式

  44. 顯示單筆資料

  45. 顯示單筆資料 • static int row_pos; • private void Button2_Click(object sender, System.EventArgs e) • { • showdata(); • row_pos++; • if(row_pos > dataSet21.Tables["employees"].Rows.Count-1) • { • row_pos=dataSet21.Tables["employees"].Rows.Count-1; • } • showdetail(); • Response.Write(row_pos); • } • private void showdetail() • { • TextBox2.Text=dataSet21.Tables["Employees"].Rows[row_pos]["Employeeid"].ToString(); • TextBox3.Text=dataSet21.Tables["Employees"].Rows[row_pos]["fIRSTName"].ToString(); • }

  46. 搜尋資料

  47. 搜尋資料 // SEARCH SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM customers where customerid LIKE'" + TextBox3.Text + "%'",sqlConnection1); da.Fill(dataSet12,"customers"); DataGrid1.DataBind();

More Related