งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

ASP [ # 11 ] เตรียมความพร้อมก่อนติดต่อกับ Ms Access

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "ASP [ # 11 ] เตรียมความพร้อมก่อนติดต่อกับ Ms Access"— ใบสำเนางานนำเสนอ:

1 ASP [ # 11 ] เตรียมความพร้อมก่อนติดต่อกับ Ms Access
การนำข้อมูลจากตารางมาแสดง การเพิ่มข้อมูลลงในตาราง การค้นหาข้อมูลจากตาราง การแก้ข้อมูลในตาราง การลบข้อมูลในตางราง การใช้ Include ประยุกต์ใช้กับฐานข้อมูล

2 ชื่อ ฐานข้อมูล product.mdb มีตาราง 2 ตารางคือ
ตัวอย่างรูปแบบการติดต่อกับ Ms Access สามารถใช้ได้กับ Access 97 และ 2000 รูปแบบจะคล้าย ๆ กันซึ่งประกอบด้วย ชื่อ ฐานข้อมูล product.mdb มีตาราง 2 ตารางคือ ชื่อตาราง TypeBook -Type_id - Type_name ชื่อตาราง product - id - barcode - description - price - page - writer -Type_id

3 ความสัมพันธ์

4 รูปแบบการติดต่อผ่าน DSNLess
ซึ่งรูปแบบการติดต่อกับฐานข้อมูลผมจะใช้แบบ DSNLess หรือถ้าหากต้องการติดต่อผ่าน ODBC ก็สามารถทำได้เหมือนกัน รูปแบบการติดต่อผ่าน DSNLess Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , ""

5 รูปแบบการติดต่อผ่าน ODBC
ทำการ Set DSN ก่อนครับ วิธี Set ดูได้จากนี้ ให้ไปที่ Win95,Win98,Me -> Start - Control Panel WinNT,2000,XP -> Start - Control Panel - Administrative Tools ให้ดับเบิ้ลคลิก  ซึ่ง windows 2 ประเภทจะไม่เหมือนกัน แต่จะคล้าย ๆ นี้ครับ

6 รูปแบบในส่วนนี้ถ้า Win95,Win98,Me ให้เลือกแท็บของ User DSN
WinNT,2000,XP ให้เลือกแท็บของ System DSN และเลือก Add จากนั้นเลือก Driver ของ Microsoft Access Driver (*.mdb) และ Finish ตามลำดับ

7 กำหนดชื่อ DSN เป็น product และทำารเลือก Drive และ Path ของฐานข้อมูล เป็นอันเสร็จกับการสร้าง DSN เพื่อใช้ในการติดต่อแบบ ODBC ต่อไปก็คือการเขียน code เพื่อติดต่อกับ DSN Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "product","",""

8 การนำข้อมูลจากตารางมาแสดง
PD_Display.asp <% Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , "" 'หรือจะใช้เป็น Conn.Open "product","","" sql ="Select * From product" Set Rs = Conn.Execute(sql) If Rs.EOF Then %> ไม่มีข้อมูลในตาราง ! <%Else%> <table border="1"> <tr bgcolor="Silver"> <td>id</td> <td>barcode</td> <td>Description</td> <td>Price</td> <td>Page</td> <td>writer</td> </tr> แทรกคำสั่งนี้ ในกรณี Join กัน 2 ตาราง ซึ่งสามารถที่จะดึงข้อมูลจากอีกตารางหนึ่ง มาแสดงได้ด้วย. sql ="Select * From TypeBook" Set Rs1 = Conn.Execute(sql)%> <Form method="Post" action="PD_display.asp"> เลือกประเภทหนังสือ : <Select name="Type"> <% While Not Rs1.EOF%> <Option value="<%= Rs1("Type_Id")%>"><%= Rs1("Type_name")%></Option> <% Rs1.MoveNext Wend Rs1.Close%> </Select> <INPUT TYPE="submit" value="แสดง"> </Form> sql ="SELECT product.*, TypeBook.type_name FROM product INNER JOIN TypeBook ON product.type_id = TypeBook.type_id where product.type_id = '" &Request.Form("Type")&"'" ComboBox ประเภท ถ้า Join 2 ตารางแล้ว ให้ลบ sql ="Select * From product“ อันเก่าออก

9 PD_display.asp (ต่อ) <% While Not Rs.EOF%> <tr> <td><%= Rs("Id")%></td> <td><%= Rs("barcode")%></td> <td><%= Rs("description")%></td> <td><%= Rs("price")%></td> <td><%= Rs("page")%></td> <td><%= Rs("writer")%></td> <td><%'= Rs("Type_name")%></td> </tr> <% Rs.MoveNext Wend Rs.Close Conn.Close End If %> </table>

10 การเพิ่มข้อมูลลงในตาราง
<% Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , "" 'หรือจะใช้เป็น Conn.Open "product","","" sql ="Select * From product" Set RS =Server.CreateObject("ADODB.Recordset") RS.open Sql,Conn,1,3 barcode="P006" description="หนังสือเล่มที่ 6" price=350 page=450 writer= "ผู้แต่งคนที่ 6" Rs.AddNew Rs("barcode") = barcode Rs("description") = description Rs("price") = price Rs("page") = page Rs("writer") = writer Rs.Update Conn.Close response.write("เพิ่มข้อมูลเรียบร้อยแล้ว") %> PD_Add006.asp ถ้า Run ไม่ได้ลองใช้

11 ประยุกต์ใช้กับ Form เพื่อเพิ่มข้อมูลลงใน DB
<html> <head> <title>กรุณากรอกข้อมูลให้ครบทุกช่องครับ</title> <body bgcolor="#FFFFFF"> <form name="form1" method="post" action="PD_Add.asp"> กรุณากรอกข้อมูล<br> <br> <table width="28%" border="0" cellspacing="0" cellpadding="0"> <tr> <td width="41%">Barcode</td> <td width="59%"> <input type="text" name="barcode"> </td> </tr> <tr> <td width="41%">Description</td> <td width="59%"> <input type="text" name="description"> </td> </tr> PD_Form.asp

12 PD_Form.asp (ต่อ) <tr> <td width="41%">Price</td> <td width="59%"> <input type="text" name="price" size="10"> </td> </tr> <tr> <td width="41%">Page</td> <td width="59%"> <input type="text" name="page" size="10"> </td> </tr> <tr> <td width="41%">Writer</td> <td width="59%"> <input type="text" name="writer"> </td> </tr> <tr> <td width="41%">Type</td> <td width="59%"> <input type="text" name=“Type_ID"> </td> </tr> </table> <br> <input type="submit" name="Submit2" value="Submit"> <input type="reset" name="Reset" value="Reset"> </form> </body> </html> Type 1

13 <% Set Conn=Server. Createobject("ADODB. Connection") Conn
<% Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , "" 'หรือจะใช้เป็น Conn.Open "product","","" sql ="Select * From product where barcode='"&Request.Form("barcode")&"'" ' ป้องกัน barcode ซ้ำ Set RS =Server.CreateObject("ADODB.Recordset") RS.open Sql,Conn,1,3 if NOT(RS.EOF) then response.write("รหัส barcode นี้มีอยู่แล้ว") else Rs.AddNew Rs("barcode") = Request.Form("barcode") Rs("description") = Request.Form("description") Rs("price") = Request.Form("price") Rs("page") = Request.Form("page") Rs("writer") = Request.Form("writer") Rs("Type_ID") = Request.Form("Type_ID") Rs.Update Conn.Close response.write("เพิ่มข้อมูลเรียบร้อยแล้ว") end if %> PD_add.asp

14 การค้นหาข้อมูลจากตาราง
PD_find.asp <html> <body> <form name="form1" method="post" action="PD_find.asp"> ค้นหาจาก Description <input type="text" name="keyword" value="<%=Request.Form("keyword")%>"> <input type="submit" name="Submit" value="ค้นหา"> </form> <% if Request.Form("keyword")="" then Response.write("<br>กรุณากรอก Keyword เพื่อค้นหาจาก Field Description<hr>") else Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , "" 'หรือจะใช้เป็น Conn.Open "product","","" sql ="Select * From product where description like '%"&Request.Form("keyword")&"%'" Set Rs = Conn.Execute(sql)

15 PD_find.asp (ต่อ) If Rs.EOF Then %> ไม่มีข้อมูลในตาราง ! <%Else%> <table border="1"> <tr bgcolor="Silver"> <td>id</td> <td>barcode</td> <td>Description</td> <td>Price</td> <td>Page</td> <td>writer</td> </tr> <% While Not Rs.EOF%> <tr> <td><%= Rs("Id")%></td> <td><%= Rs("barcode")%></td> <td><%= Rs("description")%></td> <td><%= Rs("price")%></td> <td><%= Rs("page")%></td> <td><%= Rs("writer")%></td> </tr> <% Rs.MoveNext Wend Rs.Close Conn.Close End If %> </table> <% end if %> </body> ลองสร้างการเลือกการค้นจากฟิลด์เพิ่ม นอกจาก Description โดยให้เป็น Combo box

16 การแก้ข้อมูลในตาราง ไฟล์ ไฟล์ ไฟล์ ไฟล์ PD_editList.asp
PD_EditForm.asp PD_Edit.asp <html> <body> <form name="form1" method="post" action="PD_editList.asp"> ค้นหาจาก Description <input type="text" name="keyword" value="<%=Request.Form("keyword")%>"> <input type="submit" name="Submit" value="ค้นหา"> </form> <% if Request.Form("keyword")="" then Response.write("<br>กรุณากรอก Keyword เพื่อค้นหาจาก Field Description<hr>") else Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , "" 'หรือจะใช้เป็น Conn.Open "product","","" sql ="Select * From product where description like '%"&Request.Form("keyword")&"%'" Set Rs = Conn.Execute(sql) ไฟล์ PD_editList.asp

17 If Rs. EOF Then %> ไม่มีข้อมูลในตาราง
If Rs.EOF Then %> ไม่มีข้อมูลในตาราง ! <%Else%> <table border="1"> <tr bgcolor="Silver"> <td>id</td> <td>barcode</td> <td>Description</td> <td>Price</td> <td>Page</td> <td>writer</td> <td>Edit</td> </tr> <% While Not Rs.EOF%> <tr> <td><%= Rs("Id")%></td> <td><%= Rs("barcode")%></td> <td><%= Rs("description")%></td> <td><%= Rs("price")%></td> <td><%= Rs("page")%></td> <td><%= Rs("writer")%></td> <td><a href=“PD_editform.asp?id=<%=Rs("id")%>">Edit</a></td> </tr> <% Rs.MoveNext Wend Rs.Close Conn.Close End If %> </table> <% end if %> </body> </html> PD_editList.asp (ต่อ)

18 ไฟล์ PD_editForm.asp PD_editForm.asp
<html> <head> <title>แก้ไขข้อมูล</title> <body bgcolor="#FFFFFF"> <form name="form1" method="post" action=“PD_edit.asp”> แก้ไขข้อมูล <br> <% Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , "" 'หรือจะใช้เป็น Conn.Open "product","","" sql ="Select * From product where id="&Request.QueryString("id") Set Rs = Conn.Execute(sql) %> <input type="hidden" name="id" value="<%=Rs("id")%>"> <br> <table width="28%" border="0" cellspacing="0" cellpadding="0"> PD_editForm.asp

19 <tr> <td width="41%">Barcode</td> <td width="59%"> <input type="text" name="barcode" value="<%=Rs("barcode")%>"> </td></tr> <tr> <td width="41%">Description</td> <td width="59%"> <input type="text" name="description" value="<%=Rs("description")%>"> </td></tr> <tr> <td width="41%">Price</td> <td width="59%"> <input type="text" name="price" size="10" value="<%=Rs("price")%>"> </td></tr> <tr> <td width="41%">Page</td> <td width="59%"> <input type="text" name="page" size="10" value="<%=Rs("page")%>"> </td></tr> <tr> <td width="41%">Writer</td> <td width="59%"> <input type="text" name="writer" value="<%=Rs("writer")%>"> </td></tr> </table> <br> <input type="submit" name="Submit2" value="Submit"> <input type="reset" name="Reset" value="Reset"> </form> </body> </html> PD_editForm.asp (ต่อ)

20 <% Set Conn=Server. Createobject("ADODB. Connection") Conn
<% Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , "" 'หรือจะใช้เป็น Conn.Open "product","","" sql ="Select * From product where id="&Request.Form("id") Set RS =Server.CreateObject("ADODB.Recordset") RS.open Sql,Conn,1,3 Rs("barcode") = Request.Form("barcode") Rs("description") = Request.Form("description") Rs("price") = Request.Form("price") Rs("page") = Request.Form("page") Rs("writer") = Request.Form("writer") Rs.Update Conn.Close response.write("แก้ไขข้อมูลเรียบร้อยแล้ว") %> PD_edit.asp

21 การลบข้อมูลในตางราง ให้เขียนผลลัพธ์ให้ได้ดังรูป คล้ายกับไฟล์ PD_editList.asp PD_delList.asp <a href=“PD_del.asp?id=<%=Rs("id")%>">Del</a>

22 PD_del.asp <% Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , "" 'หรือจะใช้เป็น Conn.Open "product","","" Conn.Execute "DELETE * FROM product where id="&Request.QueryString("id") Response.write("ลบข้อมูลแล้ว") Conn.Close %>

23 การใช้ Include ประยุกต์ใช้กับฐานข้อมูล
เป็นการนำเอาข้อความหรือชุดคำสั่ง ที่มีการใช้งานซ้ำกันหลาย ๆ page แยกออกมาเก็บไว้ในรูปของ File คล้ายกับ Subroutine ทำให้โปรแกรม Script มีขนาดเล็กลง และง่ายต่อการแก้ไข เช่น คำสั่งในการติดต่อกับฐานข้อมูล เราจะเขียนไฟล์อีกไฟล์หนึ่งและทุก ๆ คั้งถ้าเรามีการเรียนใช้ฐานข้อมูล เราเพียงแค่ Include ไฟล์นี้ขึ้นมาก็สามารถติดต่อกับฐานข้อมูล ทำให้ง่ายต่อการแก้ไข เมื่อมีการเปลี่ยนแปลง และแก้ไขในส่วนของการติดต่อกับฐานข้อมูล

24 ถ้าทุกไฟล์ต่อเชื่อมต่อฐานข้อมูล
a.asp DB b.asp DB c.asp DB e.asp DB d.asp DB …..asp DB ….. แต่จะยุ่งยากในการจัดการ

25 กำหนดไฟล์ Connect ไว้ที่ไฟล์เดียว
dbconnect.asp a.asp c.asp DB Include File Include File Include File Include File Include File b.asp d.asp e.asp

26 dbconnect.asp // เป็นไฟล์ที่เก็บคำสั่งในการติดต่อกับฐานข้อมูล
<% Set Conn = Server.CreateObject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/database.mdb") %> sample1.asp // เป็นไฟล์ที่มีการเรียกใช้ฐานข้อมูล เพื่อเพิ่มข้อมูลหรือแก้ไขข้อมูล <html> <body> <!-- #include file="dbconnect.asp"--> <% Sql="SELECT * FROM Data_stu" Set RS =Server.CreateObject("ADODB.Recordset") RS.open Sql,Conn,1,3 rs.AddNew rs("Name") = Request.form("name") rs("surname") = Request.form("surname") rs(" ") = Request.form(" ") rs.Update %> </html> </body>

27 sample2.asp // เป็นไฟล์ที่มีการเรียกใช้ฐานข้อมูล เพื่อลบข้อมูล
<html> <body> <!-- #include file="dbconnect.asp"--> <% delid= conn.Execute "DELETE * FROM data_stu WHERE id="&delid %> </html> </body> จากตัวอย่างจะเห็นว่า เรียกใช้ไฟล์ dbconnect.asp โดยการ <!-- #include virtual="dbconnect.asp"--> ได้เลย เป็นการเรียกใช้คำสั่งที่อยู่ในไฟล์ dbconnect.asp ประโยชน์ก็คือ ง่ายต่อการเขียน ทำให้โปรแกรมสั้นลง และง่ายต่อการแก้ไขเมื่อเราเปลี่ยน รูปแบบการติดต่อกับฐานข้อมูล


ดาวน์โหลด ppt ASP [ # 11 ] เตรียมความพร้อมก่อนติดต่อกับ Ms Access

งานนำเสนอที่คล้ายกัน


Ads by Google