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

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

ASP เตรียมความพร้อมก่อนติดต่อกับ Ms Access การนำข้อมูลจากตารางมาแสดง

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


งานนำเสนอเรื่อง: "ASP เตรียมความพร้อมก่อนติดต่อกับ Ms Access การนำข้อมูลจากตารางมาแสดง"— ใบสำเนางานนำเสนอ:

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

2 ตัวอย่าง ชื่อ ฐานข้อมูล product.mdb
ชื่อตาราง product ชื่อ Field - id - barcode - description - price - page - writer

3 การติดต่อกับฐานข้อมูลใช้แบบ DSNLess การติดต่อผ่าน ODBC
Set Conn=Server.Createobject("ADODB.Connection") Conn.Open "DRIVER=Microsoft Access Driver (*.mdb);DBQ=" & Server.MapPath("db/product.mdb"),"" , ""

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

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

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

7 การนำข้อมูลจากตารางมาแสดง
<% 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> Pro_display.asp

8 Pro_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> </tr> <% Rs.MoveNext Wend Rs.Close Conn.Close End If %> </table>

9 การเพิ่มข้อมูลลงในตาราง
<% 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 %> การเพิ่มข้อมูลลงในตาราง Pro_Add006.asp

10 ประยุกต์ใช้กับ Form เพื่อเพิ่มข้อมูลลงใน DB
<html> <head> <title>กรุณากรอกข้อมูลให้ครบทุกช่องครับ</title> <body bgcolor="#FFFFFF"> <form name="form1" method="post" action=“pro_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> Pro_Form.asp

11 Pro_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> </table> <br> <input type="submit" name="Submit2" value="Submit"> <input type="reset" name="Reset" value="Reset"> </form> </body> </html>

12 <% 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.Update Conn.Close response.write("เพิ่มข้อมูลเรียบร้อยแล้ว") end if %> Pro_add.asp

13 การค้นหาข้อมูลจากตาราง
<html> <body> <form name="form1" method="post" action="Pro_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) Pro_find.asp การค้นหาข้อมูลจากตาราง

14 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> </tr> <% While Not Rs.EOF%> <tr> <td><%= Rs("Id")%></td> <td><%= Rs("barcode")%></td> <td><%= Rs("description")%></td> Pro_find.asp (ต่อ)

15 <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> Pro_find.asp (ต่อ) ลองสร้างการเลือกการค้นจากฟิลด์เพิ่ม นอกจาก Description โดยให้เป็น Combo box

16 การแก้ข้อมูลในตาราง Pro_editList.asp
<html> <body> <form name="form1" method="post" action="Pro_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)

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> Pro_editList.asp (ต่อ)

18 <td><%= Rs("barcode")%></td> <td><%= Rs("description")%></td> <td><%= Rs("price")%></td> <td><%= Rs("page")%></td> <td><%= Rs("writer")%></td> <td><a href=“Pro_editform.asp?id=<%=Rs("id")%>">Edit</a></td> </tr> <% Rs.MoveNext Wend Rs.Close Conn.Close End If%> </table> <%end if %> </body></html> Pro_editList.asp (ต่อ)

19 <html> <head> <title>แก้ไขข้อมูล</title> <body bgcolor="#FFFFFF"> <form name="form1" method="post" action=“Pro_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"> Pro_editForm.asp

20 <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> Pro_editForm.asp (ต่อ)

21 <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> Pro_editForm.asp (ต่อ)

22 <% 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("แก้ไขข้อมูลเรียบร้อยแล้ว") %> Pro_edit.asp

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

24 <% 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","","" Conn.Execute "DELETE * FROM product where id="&Request.QueryString("id") Response.write("ลบข้อมูลแล้ว") Conn.Close %> Pro_del.asp

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

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 ง่ายต่อการแก้ไขเมื่อเราเปลี่ยน รูปแบบการติดต่อกับฐานข้อมูล

28 แบบฝึกหัด รวมรายการ เพิ่ม ค้นหา แก้ไข ลบ ข้อมูลเข้าด้วยกัน
ทำหน้าจอเพิ่มข้อมูล ให้เพิ่ม Barcode เองอัตโนมัติ โดยมี P นำหน้าด้วย รวมรายการ เพิ่ม ค้นหา แก้ไข ลบ ข้อมูลเข้าด้วยกัน

29 แบบฝึกหัด :: การค้นหาแล้วแสดงผลข้อมูลเป็นหน้า


ดาวน์โหลด ppt ASP เตรียมความพร้อมก่อนติดต่อกับ Ms Access การนำข้อมูลจากตารางมาแสดง

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


Ads by Google