SlideShare a Scribd company logo
1 of 23
Download to read offline
Create Database
Application by using
     ADO.NET

Suan Dusit Rajabhat University, Phitsanulok Campus
                           Mr.Warawut Khangkhan
Example:

            SearchPosition Projects



K.Warawut        Create Database Application by using ADO.NET   49
การสรางและใชงาน DataAdapter
 ●
     DataAdapter เป,น Object ทชวยใหเราท!างานก"บ
     ฐานขอมลในรปแบบ Disconnected Data Access
     ได
 ●
     DataAdapter ท!าหนาทเป,นต"วกลางในการร"บ-สง
     ขอมลระหวางฐานขอมลก"บ DataSet โดยบาง
     Application อาจใช DataAdapter เพยงท*ศทางเดยว
     คอ การดงขอมลจากฐานขอมลมาเก-บไวใน DataSet
     เพอใชงาน แตไมตองการ Update ขอมลกล"บลงไป
     ย"งฐานขอมล
K.Warawut        Create Database Application by using ADO.NET   50
การสรางและใชงาน DataAdapter
                  (ตอ)
 ●
     แทจร*งแลว DataAdapter สามารถท!างานได 2
     ท*ศทาง คอ นอกจากจะดงขอมลมาแลวย"งมการสง
     ขอมลไป Update ลงฐานขอมลดวย (เพราะการดง
     ขอมลเพยงอยางเดยวควรใช DataReader จะเหมาะ
     สมกวา ยกเวนวาจ!าเป,นตองเลอนเรคอร.ดกล"บไปกล"บ
     มา ซง DataReader ไมสน"บสน7น)




K.Warawut        Create Database Application by using ADO.NET   51
การสรางและใชงาน DataAdapter
                (ตอ)
●
    โดยท"วไปเราจะใช DataAdapter Object หนงๆ ท!า
    หนาทร"บ-สงขอมลระหวาง Table หนงในฐานขอมล
    ก"บ Table หนง (DataTable Object) ใน DataSet
    ด"งน"1นกรณทตองการท!างานก"บหลาย Table ก-จะใช
    DataAdapter มากกวา 1 Object




                                                  52
การสรางและใชงาน DataAdapter
                  (ตอ)
 ●
     แทจร*งแลว DataAdapter สามารถท!างานได 2
     ท*ศทาง คอ นอกจากจะดงขอมลมาแลวย"งมการสง
     ขอมลไป Update ลงฐานขอมลดวย (เพราะการดง
     ขอมลเพยงอยางเดยวควรใช DataReader จะเหมาะ
     สมกวา ยกเวนวาจ!าเป,นตองเลอนเรคอร.ดกล"บไปกล"บ
     มา ซง DataReader ไมสน"บสน7น)




K.Warawut        Create Database Application by using ADO.NET   53
การสรางและใชงาน DataAdapter
                  (ตอ)
สราง DataAdapter
 ●
     DataAdapter ก-คอออบเจ-กต.ของคลาส
     OleDbDataAdapter, SqlDataAdapter,
     OracleDataAdapter หรอ OdbcDataAdapter ข1น
     อยก"บชน*ดของ Data Provider ทใช

            Dim da As New OleDb.OleDbDataAdapter




K.Warawut           Create Database Application by using ADO.NET   54
การสรางและใชงาน DataAdapter
                  (ตอ)
สราง DataAdapter (ตอ)
 ●
     หล"งจากสราง DataAdapter ข1นมาแลว ใหสราง
     Command Object จ!านวน 4 Objects เพอน!ามา
     ก!าหนดใหพร-อพเพอร.ต1 SelectCommand,
     InsertCommand, DeleteCommand และ
     UpdateCommand ของ DataAdapter โดยตองก!า
     หนดพร-อพเพอร.ต1 เชน Connection,
     CommandText และ Commandtype ของ
     Command Object ท"1ง 4 ไวใหเรยบรอย
K.Warawut        Create Database Application by using ADO.NET   55
การสรางและใชงาน DataAdapter
                  (ตอ)
SelectCommand Properties
 ●
     ใชเก-บ Command Object ทมหนาทดงขอมลจาก
     ฐานขอมลมาใสลง DataSet
      Dim strSQL As String = “SELECT * FROM Orders”
      Dim cm As New OleDb.OleDbCommand(strSQL, cn)
      Dim da As New OleDb.OleDbDataAdapter
      da.SelectCommand = cm
                                         or
      Dim strSQL As String = “SELECT * FROM Orders”
      Dim da As New OleDb.OleDbDataAdapter
      da.SelectCommand = New OleDb.OleDbCommand(strSQL, cn)

K.Warawut            Create Database Application by using ADO.NET   56
การสรางและใชงาน DataAdapter
                  (ตอ)
SelectCommand Properties
 ●
     ใชเก-บ Command Object ทมหนาทดงขอมลจาก
     ฐานขอมลมาใสลง DataSet
      Dim strSQL As String = “SELECT * FROM Orders”
      Dim cm As New OleDb.OleDbCommand(strSQL, cn)
      Dim da As New OleDb.OleDbDataAdapter
      da.SelectCommand = cm
                                         or
      Dim strSQL As String = “SELECT * FROM Orders”
      Dim da As New OleDb.OleDbDataAdapter
      da.SelectCommand = New OleDb.OleDbCommand(strSQL, cn)

K.Warawut            Create Database Application by using ADO.NET   57
การสรางและใชงาน DataAdapter
                    (ตอ)
 SelectCommand Properties (ตอ)
  ●
       เมอสราง Command Object เก-บไวใน
       SelectCommand Properties ของ DataAdapter
       แลว เราสามารถท!างานก"บ Command Object น"1น
       โดยอางถง SelectCommand Properties ไดเลย
      da.SelectCommand.CommandText = “SELECT * FROM Products”
หรออาจประกาศตวแปรออบเจกตเพออางอ,งไปยง Command Object ใน SelectCommand ก$อน
      Dim com = da.SelectCommand
      com.CommandText = “sp_select_all_products”
      com.CommandType = CommandType.StoredProcedure
 K.Warawut               Create Database Application by using ADO.NET    58
การสรางและใชงาน DataAdapter
                  (ตอ)
Fill Method
 ●
     เป,นการดงขอมลจากฐานขอมลมาเก-บไวใน DataSet
     แลวป4ดการเชอมตอก"บฐานขอมลท"นททดงขอมล
     เสร-จ
 ●
     Fill Method ของ DataAdapter เมธอดน1จะเป4ดการ
     เชอมตอก"บฐานขอมล
 ●
     ขอมลใน DataSet ท Fill Method น!ามาใสจะมรป
     แบบเป,นตารางขอมลหรอเทเบ*ลเชนก"บฐานขอมล

K.Warawut         Create Database Application by using ADO.NET   59
การสรางและใชงาน DataAdapter
                  (ตอ)
 ●
     DataSet สามารถมเทเบ*ลไดมากกวา 1 เทเบ*ล จง
     อาจมองวา DataSet ก-คอ ฐานขอมลทเก-บอยใน
     หนวยความจ!าของเครอง Client น"นเอง เพยงแต
     DataSet อาจมขอมลท"1งหมดหรอเฉพาะสวนใดสวน
     หนงในฐานขอมลจร*งๆ ก-ได ซงข1นอยก"บวาจะดง
     ขอมลอะไรมาเก-บไวใน DataSet บาง




K.Warawut        Create Database Application by using ADO.NET   60
การสรางและใชงาน DataAdapter
                  (ตอ)
รปแบบการใชงานของเมธอด Fill
  dataadapter.Fill(dataset, table)


  dataadapter ออบเจกต DataAdapter
  dataset     ออบเจกต DataSet ท DataAdapter
              จะนาขอม"ลมาเกบไว
  table       ชอเทเบ,ลใน DataSet ทใชเกบขอม"ล ซ!งถาใน
              DataSet ยงไม$มเทเบ,ลชอน/น กจะเป0น
              การสรางเทเบ,ลใน DataSet ข!/นมาใหม$
K.Warawut              Create Database Application by using ADO.NET   61
การสรางและใชงาน DataAdapter
                  (ตอ)
ตวอยาง
  Dim cn As New OleDb.OleDbConnection( _
              “Provider=Microsoft.Jet.OleDB.4.0;” & _
              “Data Source=C:'NMID.MDB”
  Dim strSQL As String = “SELECT * FROM Employee”
  Dim da as New OleDb.OleDbDataAdapter
  da.SelectCommand = New OleDb.OleDbCommand(strSQL, cn)

  Dim da As New DataSet
  da.Fill(ds, “Employee”)




K.Warawut            Create Database Application by using ADO.NET   62
การสรางและใชงาน DataAdapter
                  (ตอ)
อางถงฟ!ลดตางๆ ใน DataSet เพอท-างานกบขอมล
  dataset.Tables(table).Rows(row)(field)

  dataset       ออบเจกต DataSet
  table         ชอหรอหมายเลขลาดบของเทเบ,ลใน DataSet
  row           หมายเลขลาดบของเรคอรด
  field         ชอหรอหมายเลขลาดบของฟลด
    TableBox1.Text = ds.Table(“Employee”).Row(0)(“FirstName”)

    ds.Table(“Employee”).Row(0)(“LastName”) = “Khangkhan”

K.Warawut             Create Database Application by using ADO.NET   63
การสรางและใชงาน DataAdapter
                (ตอ)
ใช CommandBuilder สราง Command Object
อนๆ
●
    การ Update ฐานขอมลน"1น DataAdapter จะอาศ"ย
    ออบเจ-กต. Command ทอยในพร-อพเพอร.ต1
    InsertCommand, DeleteCommand และ
    UpdateCommand ของม"น
●
    ออบเจ-กต. CommandBuilder จะพ*จารณาค!าส"ง
    SQL ของออบเจ-กต. Command ใน
    SelectCommand แลวสรางค!าส"ง SQL ใหก"บ 3
    ออบเจ-กต.ทเหลอโดยอ"ตโนม"ต*                   64
การสรางและใชงาน DataAdapter
                   (ตอ)
การสราง CommandBuilder
 ●
      สราง CommandBuilder ข1นมาจากคลาสทเหมาะสม
      (ข1นอยก"บชน*ด Data Provider) พรอมท"1งระบ7
      DataAdapter เป,นอาร.ก*วเมนต.ใหก"บคอนสตร"คเตอร.
      ของคลาส
     Dim cb As New OleDb.OleDbCommandBuilder(da)
     ไมตองเรยกใชพรอพเพอรต'หรอเมธอดใดของ CommandBuilder



K.Warawut             Create Database Application by using ADO.NET   65
การสรางและใชงาน DataAdapter
                  (ตอ)
Update Method
 ●
     เมธอด Update ของ DataAdapter ใชอ"พเดลขอมล
     จาก DataSet ลงสฐานขอมล โดย DataSet จะคอย
     จดจ!าวามการเพ*ม ลบ และแกไขเรคอร.ดอะไรบาง
     เพอให DataAdapter สามารถอ"พเดลฐานขอมลได
     อยางถกตอง




K.Warawut        Create Database Application by using ADO.NET   66
การสรางและใชงาน DataAdapter
                  (ตอ)
รปแบบการใชงานของเมธอด Update
  dataadapter.Update(dataset, table)


  dataadapter ออบเจกต DataAdapter
  dataset     ออบเจกต DataSet ท DataAdapter
              จะนาขอม"ลไปอพเดลลงฐานขอม"ล
  table       ชอเทเบ,ลใน DataSet ทคณตองการใหนาขอม"ลไป
              อพเดลลงฐานขอม"ล
    ds.Update(ds, “Employee”)

K.Warawut            Create Database Application by using ADO.NET   67
Example:

        DataAdapterDemo1 Projects



K.Warawut      Create Database Application by using ADO.NET   68
Homework:

        DataAdapterDemo2 Projects
          Select index in Listbox1



K.Warawut      Create Database Application by using ADO.NET   69
Contact us
            Mr.Warawut Khangkhan
 ●   Social Media:
     http://www.facebook.com/awarawut (Profile)
     http://www.facebook.com/AjWarawut (Learning)
     http://twitter.com/awarawut
 ●   Web Site:
     http://awarawut.blogspot.com
 ●   E-Mail:
     awarawut@hotmail.com
     warawut_kha@dusit.ac.th
 ●   Mobile:
     083-0698-410
K.Warawut           Create Database Application by using ADO.NET   70

More Related Content

Similar to 03 3-create-db-application-ado-dot-net Create Database Application with using ADO.NET

ADO.NET Entity Framework 4
ADO.NET Entity Framework 4ADO.NET Entity Framework 4
ADO.NET Entity Framework 4Raffaele Fanizzi
 
Entity+framework+
Entity+framework+Entity+framework+
Entity+framework+Rey zhang
 
Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 zhen chen
 
Boostのあるプログラミング生活
Boostのあるプログラミング生活Boostのあるプログラミング生活
Boostのあるプログラミング生活Akira Takahashi
 
Core rest edgarsilva_v1
Core rest edgarsilva_v1Core rest edgarsilva_v1
Core rest edgarsilva_v1Edgar Silva
 
No sql pour valtech tech days
No sql pour valtech tech daysNo sql pour valtech tech days
No sql pour valtech tech daysClaude Falguiere
 
N03 app engineseminar
N03 app engineseminarN03 app engineseminar
N03 app engineseminarSun-Jin Jang
 
VENU_Hadoop_Resume
VENU_Hadoop_ResumeVENU_Hadoop_Resume
VENU_Hadoop_ResumeVenu Gopal
 
Aspetos gerais de desenvolvimento web.
Aspetos gerais de desenvolvimento web.Aspetos gerais de desenvolvimento web.
Aspetos gerais de desenvolvimento web.Corcioli
 
Uso de la Programación para la Seguridad en Redes (a.k.a. Sockets y Shellcoding)
Uso de la Programación para la Seguridad en Redes (a.k.a. Sockets y Shellcoding)Uso de la Programación para la Seguridad en Redes (a.k.a. Sockets y Shellcoding)
Uso de la Programación para la Seguridad en Redes (a.k.a. Sockets y Shellcoding)Pedro Valera
 
C++模板与泛型编程
C++模板与泛型编程C++模板与泛型编程
C++模板与泛型编程deer hope
 
Webperfdays 2014 - common web performance mistakes
Webperfdays 2014  - common web performance mistakesWebperfdays 2014  - common web performance mistakes
Webperfdays 2014 - common web performance mistakesdkoston
 
C++模板与泛型编程
C++模板与泛型编程C++模板与泛型编程
C++模板与泛型编程deer hope
 
Nosql七种武器之长生剑 mongodb的使用介绍
Nosql七种武器之长生剑 mongodb的使用介绍Nosql七种武器之长生剑 mongodb的使用介绍
Nosql七种武器之长生剑 mongodb的使用介绍thinkinlamp
 
Neu bei der DOI-Registrierung? Ihr Weg zum DOI! - DataCite 2014
Neu bei der DOI-Registrierung? Ihr Weg zum DOI! - DataCite 2014Neu bei der DOI-Registrierung? Ihr Weg zum DOI! - DataCite 2014
Neu bei der DOI-Registrierung? Ihr Weg zum DOI! - DataCite 2014Frauke Ziedorn
 
Seguridad en Servicios Web
Seguridad en Servicios WebSeguridad en Servicios Web
Seguridad en Servicios WebHacking Bolivia
 
Mini charla jquery
Mini charla jqueryMini charla jquery
Mini charla jquerylizardoceliz
 

Similar to 03 3-create-db-application-ado-dot-net Create Database Application with using ADO.NET (19)

ADO.NET Entity Framework 4
ADO.NET Entity Framework 4ADO.NET Entity Framework 4
ADO.NET Entity Framework 4
 
Entity+framework+
Entity+framework+Entity+framework+
Entity+framework+
 
Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储 Ria的强力后盾:rest+海量存储
Ria的强力后盾:rest+海量存储
 
Boostのあるプログラミング生活
Boostのあるプログラミング生活Boostのあるプログラミング生活
Boostのあるプログラミング生活
 
Core rest edgarsilva_v1
Core rest edgarsilva_v1Core rest edgarsilva_v1
Core rest edgarsilva_v1
 
No sql pour valtech tech days
No sql pour valtech tech daysNo sql pour valtech tech days
No sql pour valtech tech days
 
MS Swit 2010
MS Swit 2010MS Swit 2010
MS Swit 2010
 
N03 app engineseminar
N03 app engineseminarN03 app engineseminar
N03 app engineseminar
 
2011 01-18 mongo-db
2011 01-18 mongo-db2011 01-18 mongo-db
2011 01-18 mongo-db
 
VENU_Hadoop_Resume
VENU_Hadoop_ResumeVENU_Hadoop_Resume
VENU_Hadoop_Resume
 
Aspetos gerais de desenvolvimento web.
Aspetos gerais de desenvolvimento web.Aspetos gerais de desenvolvimento web.
Aspetos gerais de desenvolvimento web.
 
Uso de la Programación para la Seguridad en Redes (a.k.a. Sockets y Shellcoding)
Uso de la Programación para la Seguridad en Redes (a.k.a. Sockets y Shellcoding)Uso de la Programación para la Seguridad en Redes (a.k.a. Sockets y Shellcoding)
Uso de la Programación para la Seguridad en Redes (a.k.a. Sockets y Shellcoding)
 
C++模板与泛型编程
C++模板与泛型编程C++模板与泛型编程
C++模板与泛型编程
 
Webperfdays 2014 - common web performance mistakes
Webperfdays 2014  - common web performance mistakesWebperfdays 2014  - common web performance mistakes
Webperfdays 2014 - common web performance mistakes
 
C++模板与泛型编程
C++模板与泛型编程C++模板与泛型编程
C++模板与泛型编程
 
Nosql七种武器之长生剑 mongodb的使用介绍
Nosql七种武器之长生剑 mongodb的使用介绍Nosql七种武器之长生剑 mongodb的使用介绍
Nosql七种武器之长生剑 mongodb的使用介绍
 
Neu bei der DOI-Registrierung? Ihr Weg zum DOI! - DataCite 2014
Neu bei der DOI-Registrierung? Ihr Weg zum DOI! - DataCite 2014Neu bei der DOI-Registrierung? Ihr Weg zum DOI! - DataCite 2014
Neu bei der DOI-Registrierung? Ihr Weg zum DOI! - DataCite 2014
 
Seguridad en Servicios Web
Seguridad en Servicios WebSeguridad en Servicios Web
Seguridad en Servicios Web
 
Mini charla jquery
Mini charla jqueryMini charla jquery
Mini charla jquery
 

More from Warawut

Database design
Database designDatabase design
Database designWarawut
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4Warawut
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10Warawut
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9Warawut
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8Warawut
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7Warawut
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6Warawut
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6Warawut
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5Warawut
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4Warawut
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5Warawut
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3Warawut
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3Warawut
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2Warawut
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemWarawut
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4Warawut
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1Warawut
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MISWarawut
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3Warawut
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2Warawut
 

More from Warawut (20)

Database design
Database designDatabase design
Database design
 
Business Computer Project 4
Business Computer Project 4Business Computer Project 4
Business Computer Project 4
 
Object-Oriented Programming 10
Object-Oriented Programming 10Object-Oriented Programming 10
Object-Oriented Programming 10
 
Object-Oriented Programming 9
Object-Oriented Programming 9Object-Oriented Programming 9
Object-Oriented Programming 9
 
Object-Oriented Programming 8
Object-Oriented Programming 8Object-Oriented Programming 8
Object-Oriented Programming 8
 
Object-Oriented Programming 7
Object-Oriented Programming 7Object-Oriented Programming 7
Object-Oriented Programming 7
 
Object-Oriented Programming 6
Object-Oriented Programming 6Object-Oriented Programming 6
Object-Oriented Programming 6
 
Management Information System 6
Management Information System 6Management Information System 6
Management Information System 6
 
Management Information System 5
Management Information System 5Management Information System 5
Management Information System 5
 
Management Information System 4
Management Information System 4Management Information System 4
Management Information System 4
 
Object-Oriented Programming 5
Object-Oriented Programming 5Object-Oriented Programming 5
Object-Oriented Programming 5
 
Business Computer Project 3
Business Computer Project 3Business Computer Project 3
Business Computer Project 3
 
Management Information System 3
Management Information System 3Management Information System 3
Management Information System 3
 
Business Computer Project 2
Business Computer Project 2Business Computer Project 2
Business Computer Project 2
 
Chapter 2 Strategy & Information System
Chapter 2 Strategy & Information SystemChapter 2 Strategy & Information System
Chapter 2 Strategy & Information System
 
Object-Oriented Programming 4
Object-Oriented Programming 4Object-Oriented Programming 4
Object-Oriented Programming 4
 
Business Computer Project 1
Business Computer Project 1Business Computer Project 1
Business Computer Project 1
 
Chapter 1 Organization & MIS
Chapter 1 Organization & MISChapter 1 Organization & MIS
Chapter 1 Organization & MIS
 
Object-Oriented Programming 3
Object-Oriented Programming 3Object-Oriented Programming 3
Object-Oriented Programming 3
 
Object-Oriented Programming 2
Object-Oriented Programming 2Object-Oriented Programming 2
Object-Oriented Programming 2
 

03 3-create-db-application-ado-dot-net Create Database Application with using ADO.NET

  • 1. Create Database Application by using ADO.NET Suan Dusit Rajabhat University, Phitsanulok Campus Mr.Warawut Khangkhan
  • 2. Example: SearchPosition Projects K.Warawut Create Database Application by using ADO.NET 49
  • 3. การสรางและใชงาน DataAdapter ● DataAdapter เป,น Object ทชวยใหเราท!างานก"บ ฐานขอมลในรปแบบ Disconnected Data Access ได ● DataAdapter ท!าหนาทเป,นต"วกลางในการร"บ-สง ขอมลระหวางฐานขอมลก"บ DataSet โดยบาง Application อาจใช DataAdapter เพยงท*ศทางเดยว คอ การดงขอมลจากฐานขอมลมาเก-บไวใน DataSet เพอใชงาน แตไมตองการ Update ขอมลกล"บลงไป ย"งฐานขอมล K.Warawut Create Database Application by using ADO.NET 50
  • 4. การสรางและใชงาน DataAdapter (ตอ) ● แทจร*งแลว DataAdapter สามารถท!างานได 2 ท*ศทาง คอ นอกจากจะดงขอมลมาแลวย"งมการสง ขอมลไป Update ลงฐานขอมลดวย (เพราะการดง ขอมลเพยงอยางเดยวควรใช DataReader จะเหมาะ สมกวา ยกเวนวาจ!าเป,นตองเลอนเรคอร.ดกล"บไปกล"บ มา ซง DataReader ไมสน"บสน7น) K.Warawut Create Database Application by using ADO.NET 51
  • 5. การสรางและใชงาน DataAdapter (ตอ) ● โดยท"วไปเราจะใช DataAdapter Object หนงๆ ท!า หนาทร"บ-สงขอมลระหวาง Table หนงในฐานขอมล ก"บ Table หนง (DataTable Object) ใน DataSet ด"งน"1นกรณทตองการท!างานก"บหลาย Table ก-จะใช DataAdapter มากกวา 1 Object 52
  • 6. การสรางและใชงาน DataAdapter (ตอ) ● แทจร*งแลว DataAdapter สามารถท!างานได 2 ท*ศทาง คอ นอกจากจะดงขอมลมาแลวย"งมการสง ขอมลไป Update ลงฐานขอมลดวย (เพราะการดง ขอมลเพยงอยางเดยวควรใช DataReader จะเหมาะ สมกวา ยกเวนวาจ!าเป,นตองเลอนเรคอร.ดกล"บไปกล"บ มา ซง DataReader ไมสน"บสน7น) K.Warawut Create Database Application by using ADO.NET 53
  • 7. การสรางและใชงาน DataAdapter (ตอ) สราง DataAdapter ● DataAdapter ก-คอออบเจ-กต.ของคลาส OleDbDataAdapter, SqlDataAdapter, OracleDataAdapter หรอ OdbcDataAdapter ข1น อยก"บชน*ดของ Data Provider ทใช Dim da As New OleDb.OleDbDataAdapter K.Warawut Create Database Application by using ADO.NET 54
  • 8. การสรางและใชงาน DataAdapter (ตอ) สราง DataAdapter (ตอ) ● หล"งจากสราง DataAdapter ข1นมาแลว ใหสราง Command Object จ!านวน 4 Objects เพอน!ามา ก!าหนดใหพร-อพเพอร.ต1 SelectCommand, InsertCommand, DeleteCommand และ UpdateCommand ของ DataAdapter โดยตองก!า หนดพร-อพเพอร.ต1 เชน Connection, CommandText และ Commandtype ของ Command Object ท"1ง 4 ไวใหเรยบรอย K.Warawut Create Database Application by using ADO.NET 55
  • 9. การสรางและใชงาน DataAdapter (ตอ) SelectCommand Properties ● ใชเก-บ Command Object ทมหนาทดงขอมลจาก ฐานขอมลมาใสลง DataSet Dim strSQL As String = “SELECT * FROM Orders” Dim cm As New OleDb.OleDbCommand(strSQL, cn) Dim da As New OleDb.OleDbDataAdapter da.SelectCommand = cm or Dim strSQL As String = “SELECT * FROM Orders” Dim da As New OleDb.OleDbDataAdapter da.SelectCommand = New OleDb.OleDbCommand(strSQL, cn) K.Warawut Create Database Application by using ADO.NET 56
  • 10. การสรางและใชงาน DataAdapter (ตอ) SelectCommand Properties ● ใชเก-บ Command Object ทมหนาทดงขอมลจาก ฐานขอมลมาใสลง DataSet Dim strSQL As String = “SELECT * FROM Orders” Dim cm As New OleDb.OleDbCommand(strSQL, cn) Dim da As New OleDb.OleDbDataAdapter da.SelectCommand = cm or Dim strSQL As String = “SELECT * FROM Orders” Dim da As New OleDb.OleDbDataAdapter da.SelectCommand = New OleDb.OleDbCommand(strSQL, cn) K.Warawut Create Database Application by using ADO.NET 57
  • 11. การสรางและใชงาน DataAdapter (ตอ) SelectCommand Properties (ตอ) ● เมอสราง Command Object เก-บไวใน SelectCommand Properties ของ DataAdapter แลว เราสามารถท!างานก"บ Command Object น"1น โดยอางถง SelectCommand Properties ไดเลย da.SelectCommand.CommandText = “SELECT * FROM Products” หรออาจประกาศตวแปรออบเจกตเพออางอ,งไปยง Command Object ใน SelectCommand ก$อน Dim com = da.SelectCommand com.CommandText = “sp_select_all_products” com.CommandType = CommandType.StoredProcedure K.Warawut Create Database Application by using ADO.NET 58
  • 12. การสรางและใชงาน DataAdapter (ตอ) Fill Method ● เป,นการดงขอมลจากฐานขอมลมาเก-บไวใน DataSet แลวป4ดการเชอมตอก"บฐานขอมลท"นททดงขอมล เสร-จ ● Fill Method ของ DataAdapter เมธอดน1จะเป4ดการ เชอมตอก"บฐานขอมล ● ขอมลใน DataSet ท Fill Method น!ามาใสจะมรป แบบเป,นตารางขอมลหรอเทเบ*ลเชนก"บฐานขอมล K.Warawut Create Database Application by using ADO.NET 59
  • 13. การสรางและใชงาน DataAdapter (ตอ) ● DataSet สามารถมเทเบ*ลไดมากกวา 1 เทเบ*ล จง อาจมองวา DataSet ก-คอ ฐานขอมลทเก-บอยใน หนวยความจ!าของเครอง Client น"นเอง เพยงแต DataSet อาจมขอมลท"1งหมดหรอเฉพาะสวนใดสวน หนงในฐานขอมลจร*งๆ ก-ได ซงข1นอยก"บวาจะดง ขอมลอะไรมาเก-บไวใน DataSet บาง K.Warawut Create Database Application by using ADO.NET 60
  • 14. การสรางและใชงาน DataAdapter (ตอ) รปแบบการใชงานของเมธอด Fill dataadapter.Fill(dataset, table) dataadapter ออบเจกต DataAdapter dataset ออบเจกต DataSet ท DataAdapter จะนาขอม"ลมาเกบไว table ชอเทเบ,ลใน DataSet ทใชเกบขอม"ล ซ!งถาใน DataSet ยงไม$มเทเบ,ลชอน/น กจะเป0น การสรางเทเบ,ลใน DataSet ข!/นมาใหม$ K.Warawut Create Database Application by using ADO.NET 61
  • 15. การสรางและใชงาน DataAdapter (ตอ) ตวอยาง Dim cn As New OleDb.OleDbConnection( _ “Provider=Microsoft.Jet.OleDB.4.0;” & _ “Data Source=C:'NMID.MDB” Dim strSQL As String = “SELECT * FROM Employee” Dim da as New OleDb.OleDbDataAdapter da.SelectCommand = New OleDb.OleDbCommand(strSQL, cn) Dim da As New DataSet da.Fill(ds, “Employee”) K.Warawut Create Database Application by using ADO.NET 62
  • 16. การสรางและใชงาน DataAdapter (ตอ) อางถงฟ!ลดตางๆ ใน DataSet เพอท-างานกบขอมล dataset.Tables(table).Rows(row)(field) dataset ออบเจกต DataSet table ชอหรอหมายเลขลาดบของเทเบ,ลใน DataSet row หมายเลขลาดบของเรคอรด field ชอหรอหมายเลขลาดบของฟลด TableBox1.Text = ds.Table(“Employee”).Row(0)(“FirstName”) ds.Table(“Employee”).Row(0)(“LastName”) = “Khangkhan” K.Warawut Create Database Application by using ADO.NET 63
  • 17. การสรางและใชงาน DataAdapter (ตอ) ใช CommandBuilder สราง Command Object อนๆ ● การ Update ฐานขอมลน"1น DataAdapter จะอาศ"ย ออบเจ-กต. Command ทอยในพร-อพเพอร.ต1 InsertCommand, DeleteCommand และ UpdateCommand ของม"น ● ออบเจ-กต. CommandBuilder จะพ*จารณาค!าส"ง SQL ของออบเจ-กต. Command ใน SelectCommand แลวสรางค!าส"ง SQL ใหก"บ 3 ออบเจ-กต.ทเหลอโดยอ"ตโนม"ต* 64
  • 18. การสรางและใชงาน DataAdapter (ตอ) การสราง CommandBuilder ● สราง CommandBuilder ข1นมาจากคลาสทเหมาะสม (ข1นอยก"บชน*ด Data Provider) พรอมท"1งระบ7 DataAdapter เป,นอาร.ก*วเมนต.ใหก"บคอนสตร"คเตอร. ของคลาส Dim cb As New OleDb.OleDbCommandBuilder(da) ไมตองเรยกใชพรอพเพอรต'หรอเมธอดใดของ CommandBuilder K.Warawut Create Database Application by using ADO.NET 65
  • 19. การสรางและใชงาน DataAdapter (ตอ) Update Method ● เมธอด Update ของ DataAdapter ใชอ"พเดลขอมล จาก DataSet ลงสฐานขอมล โดย DataSet จะคอย จดจ!าวามการเพ*ม ลบ และแกไขเรคอร.ดอะไรบาง เพอให DataAdapter สามารถอ"พเดลฐานขอมลได อยางถกตอง K.Warawut Create Database Application by using ADO.NET 66
  • 20. การสรางและใชงาน DataAdapter (ตอ) รปแบบการใชงานของเมธอด Update dataadapter.Update(dataset, table) dataadapter ออบเจกต DataAdapter dataset ออบเจกต DataSet ท DataAdapter จะนาขอม"ลไปอพเดลลงฐานขอม"ล table ชอเทเบ,ลใน DataSet ทคณตองการใหนาขอม"ลไป อพเดลลงฐานขอม"ล ds.Update(ds, “Employee”) K.Warawut Create Database Application by using ADO.NET 67
  • 21. Example: DataAdapterDemo1 Projects K.Warawut Create Database Application by using ADO.NET 68
  • 22. Homework: DataAdapterDemo2 Projects Select index in Listbox1 K.Warawut Create Database Application by using ADO.NET 69
  • 23. Contact us Mr.Warawut Khangkhan ● Social Media: http://www.facebook.com/awarawut (Profile) http://www.facebook.com/AjWarawut (Learning) http://twitter.com/awarawut ● Web Site: http://awarawut.blogspot.com ● E-Mail: awarawut@hotmail.com warawut_kha@dusit.ac.th ● Mobile: 083-0698-410 K.Warawut Create Database Application by using ADO.NET 70