ADO.NET
database connection

@wong
Ref:: http://www.anekwong.com
ADO.NET
• ADO.NET
.NET
• ADO => ActiveX Data Object

.NET
ADO NET Architecture
• NET Framework data providers
• The DataSet
windows Data Sources
Windows Data sources
(data source)

-

-

IDE

,

(bind)
Data Provider
•

Query
•

Object

DataSet
•

VS2005
–
–
–
–

SQL Server Data Provider
OLEDB Data Provider
Oracle Data Prov...
Data Provider
• Data Provider
– Connection
– Command
Query
SQL
– DataAdapter
fill
DataSet
– DataReader
result set
read-onl...
NET Framework Data Providers
NET Framework
data provider

Description

NET Framework Data Provider for SQL Server

Provide...
Core Objects of NET Framework Data Providers

Object Description
Connection

Establishes a connection to a specific data s...
- The NET Framework Data Provider for SQL Server

Imports Provider
Imports System Data SqlClient
- The .NET Framework Data...
- The NET Framework Data Provider for ODBC

Imports Provider
Imports System Data Odbc
Driver
SQL Server
Microsoft ODBC for...
Choosing a NET Framework Data Provider
Provider

Notes

NET Framework
Data Provider
for SQL Server

Recommended for middle...
2. Data Set Designer
Vs
DataSet

.Net Framework
disconnected data access

(

Data Table)
)

DataSet (
DataSet
ADO NET DataSet
dataSet
Data Table

Customers
Orders

Table Adapter
SME
Table Adapter

Customer
Orders
DataSet
•
DataSet

Disconnected data access
•
– DataTable
dataSet

TableAdapter

TableAdapter
DataSet
DataSet

Tableadapter.Fill(datatable)

Tableadapter
Datatable

DataSet
DataTab...
Dataset.table(row).field
Dataset
Table
Row
count-1
Field

Dataset

count
Tableadapter.Fill(datatable)
Fill

GetData
GetData
DataTable

TableAdap
D
GetData
Data Binding
Data Binding

(bind)

DataBinding
code

Dat

Dat
ADO NET Sample Application
Option Explicit On
Option Strict On

Provider
SqlClient

Imports System
Imports System Data
Imp...
Try
connection Open
Dim dataReader As SqlDataReader _
command ExecuteReader
Do While dataReader Read
Console WriteLine vbT...
ADO NET Sample Application
Option Explicit On
Option Strict On

Provider

Imports System
Imports System Data
Imports Syste...
Try
connection Open
Dim dataReader As OleDbDataReader _
command ExecuteReader
Do While dataReader Read
Console WriteLine v...
Just do it!
•
•

OleDB Provider

Northwind
Ms Access
Import
Provider

Imports System.Data.OleDB
Dim objConn As New System....
objConn.Open()

objConn.Close()
Command
Sql
Select, Update, Delete, Insert

Dim objComm As New OleDb.OleDbCommand()

Provider
CommandText
objComm.CommandT...
Command
Sql
Select, Update, Delete, Insert

Dim objComm As New OleDb.OleDbCommand()

Provider
CommandText
objComm.CommandT...
objComm.ExecuteReader
Set

dataReader
Dim dr As OleDb.OleDbDataReader
dr = objComm.ExcuteReader

Item

DataReader

While d...
objComm.ExecuteReader
Set

dataReader
Dim dr As OleDb.OleDbDataReader
dr = objComm.ExcuteReader

Item

DataReader

While d...
DataReader
Click

ListBox1
Dim strSQL As String = “Select CustomerID,CompanyName F
Sub
Dim objConn As New OleDbConnection(StrConn)
Dim objComm As New...
DataReader
DataAdapter
DataAdapter => Dim da As New OleDb.OleDbDataAdapter
Method Fill

DataSet => dataAdapter.Fill(dataSet,tab

Dim ...
DataAdapter
Method Update
=> dataAdapter.Update(dataSet,table)
DataSet
•

DataSet
Dim tb As DataTable = ds.Tables(“Employees”)
Dim row As DataRow = tb.NewRow()
row(“FirstName”) = “Chaim...


DataSet
Dim tb As DataTable = ds.Tables(“Employees”)
For Each row As DataRow In tb.Rows
if row(“FirstName”) = “Chaimard...


DataSet
Dim tb As DataTable = ds.Tables(“Employees”)
For Each row As DataRow In tb.Rows
if row(“FirstName”) = “Chaimard...
Binding
Data
• Simple Binding Data
textbox, Label

• Complex Binding Data
ListBox, ComboBox


Simple Binding Data
:
object.DataBindings.Add(propertyName,dataSource,dataMember)
: textbox1.dataBindings.Add(“text”,ds...

: me.bindingContext(ds.Tables.(“emp”)).position = 0
: me.bindingContext(ds.Tables.(“emp”)).position +=
1

: me.bindingCo...
Upcoming SlideShare
Loading in …5
×

ADO.NET -database connection

1,975 views

Published on

การเขียนโปรแกรมฐานข้อมูล
1. ใช้โปรแกรมฐานข้อมูลเขียนหนาจอ เชน Access, Oracle
ข้อดี คือ เมื่อเขียนแล้วจะเกิด error น้อย
ข้อเสีย คือ รูปแบบหน้าจอไม่ยืดหยุ่น
2. ใช้โปรแกรมภาษาเขียนติดต่อฐานข้อมูล เช่น VB, Delphi, Java
ข้อดี : หน้าจอเป็นไปตามความสามารถของโปรแกรมเมอร์
ข้อเสีย : เขียนแล้วจะเกิด error ได้ง่าย
Mysql MsSQL จะไม่มี หน้าจอ จึงต้องใช้วิธีที่สองเท่านั้น

การเขียนโปรแกรมติดตอฐานขอมูลโดยใช VB มี 3 เทคนิค
1. DAO
2. RDO
3. ADO
4. ADO.NET (เฉพาะ VB.NET) จะใช้เมื่อเขียนแบบ Web Application
มีวิธีในการเขียนโปรแกรมเพื่อติดต่อฐานข้อมูล 2 วิธี
1. Control
2. Coding
3. Control + Coding เป็นการผสมระหว่าง 1 และ 2

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,975
On SlideShare
0
From Embeds
0
Number of Embeds
56
Actions
Shares
0
Downloads
118
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

ADO.NET -database connection

  1. 1. ADO.NET database connection @wong Ref:: http://www.anekwong.com
  2. 2. ADO.NET • ADO.NET .NET • ADO => ActiveX Data Object .NET
  3. 3. ADO NET Architecture • NET Framework data providers • The DataSet
  4. 4. windows Data Sources Windows Data sources (data source) - - IDE , (bind)
  5. 5. Data Provider • Query • Object DataSet • VS2005 – – – – SQL Server Data Provider OLEDB Data Provider Oracle Data Provider ODBC Data Provider MS SQL Server MS Access V.7
  6. 6. Data Provider • Data Provider – Connection – Command Query SQL – DataAdapter fill DataSet – DataReader result set read-only forward-only /
  7. 7. NET Framework Data Providers NET Framework data provider Description NET Framework Data Provider for SQL Server Provides data access for Microsoft SQL Server version 7.0 or later Uses the System.Data.SqlClient namespace NET Framework Data Provider for OLE DB For data sources exposed using OLE DB Uses the System.Data.OleDb namespace NET Framework Data Provider for ODBC For data sources exposed using ODBC Uses the System.Data.Odbc namespace NET Framework Data Provider for Oracle For Oracle data sources The NET Framework Data Provider for Oracle supports Oracle client software version 8.1.7 and later, and uses the System.Data.OracleClient namespace
  8. 8. Core Objects of NET Framework Data Providers Object Description Connection Establishes a connection to a specific data source The base class for all Connection objects is the DbConnection class Command Executes a command against a data source Exposes Parameters and can execute within the scope of a Transaction from a Connection. The base class for all Command objects is the DbCommand class DataReader Reads a forward-only, read-only stream of data from a data source The base class for all DataReader objects is the DbDataReader class DataAdapter Populates a DataSet and resolves updates with the data source The base class for all DataAdapter objects is the DbDataAdapter class
  9. 9. - The NET Framework Data Provider for SQL Server Imports Provider Imports System Data SqlClient - The .NET Framework Data Provider for OLE DB Imports Provider Imports System Data OleDb Driver Provider SQLOLEDB Microsoft OLE DB Provider for SQL Server MSDAORA Microsoft OLE DB Provider for Oracle Microsoft Jet OLEDB 4.0 OLE DB Provider for Microsoft Jet
  10. 10. - The NET Framework Data Provider for ODBC Imports Provider Imports System Data Odbc Driver SQL Server Microsoft ODBC for Oracle Microsoft Access Driver mdb - The NET Framework Data Provider for Oracle Imports Provider Imports System.Data Imports System.Data.OracleClient
  11. 11. Choosing a NET Framework Data Provider Provider Notes NET Framework Data Provider for SQL Server Recommended for middle-tier applications using Microsoft SQL Server 7.0 or later Recommended for single-tier applications using Microsoft Database Engine MSDE or SQL Server 7.0 or later Recommended over use of the OLE DB Provider for SQL Server SQLOLEDB with the NET Framework Data Provider for OLE DB For SQL Server 6.5 and earlier, you must use the OLE DB Provider for SQL Server with the NET Framework Data Provider for OLE DB NET Framework Data Provider for OLE DB Recommended for middle-tier applications using SQL Server 6.5 or earlier For SQL Server 7.0 or later, the NET Framework Data Provider for SQL Server is recommended Also recommended for single-tier applications using Microsoft Access databases Use of an Access database for a middle-tier application is not recommended NET Framework Data Provider for ODBC Recommended for middle and single-tier applications using ODBC data sources NET Framework Data Provider for Oracle Recommended for middle and single-tier applications using Oracle data sources
  12. 12. 2. Data Set Designer Vs DataSet .Net Framework disconnected data access ( Data Table) ) DataSet ( DataSet
  13. 13. ADO NET DataSet
  14. 14. dataSet Data Table Customers Orders Table Adapter SME Table Adapter Customer Orders
  15. 15. DataSet • DataSet Disconnected data access • – DataTable
  16. 16. dataSet TableAdapter TableAdapter DataSet DataSet Tableadapter.Fill(datatable) Tableadapter Datatable DataSet DataTable TableAdapter Data Table
  17. 17. Dataset.table(row).field Dataset Table Row count-1 Field Dataset count
  18. 18. Tableadapter.Fill(datatable) Fill GetData GetData DataTable TableAdap D GetData
  19. 19. Data Binding Data Binding (bind) DataBinding code Dat Dat
  20. 20. ADO NET Sample Application Option Explicit On Option Strict On Provider SqlClient Imports System Imports System Data Imports System Data SqlClient Public Class Program Public Shared Sub Main Dim connectionString As String GetConnectionString Dim queryString As String _ SELECT CategoryID, CategoryName FROM dbo Categories; Using connection As New SqlConnection connectionString Dim command As SqlCommand connection CreateCommand command CommandText queryString
  21. 21. Try connection Open Dim dataReader As SqlDataReader _ command ExecuteReader Do While dataReader Read Console WriteLine vbTab & {0} & vbTab & {1} , _ dataReader 0 , dataReader 1 Loop dataReader Close Catch ex As Exception Console WriteLine ex Message End Try End Using End Sub Private Shared Function GetConnectionString As String ' To avoid storing the connection string in your code, ' you can retrieve it from a configuration file Return Data Source local ;Initial Catalog Northwind; _ & Integrated Security SSPI; End Function End Class
  22. 22. ADO NET Sample Application Option Explicit On Option Strict On Provider Imports System Imports System Data Imports System Data OleDb Public Class Program Public Shared Sub Main Dim connectionString As String GetConnectionString Dim queryString As String _ SELECT CategoryID, CategoryName FROM Categories; Using connection As New OleDbConnection connectionString Dim command As OleDbCommand connection CreateCommand command CommandText queryString Try OleDb
  23. 23. Try connection Open Dim dataReader As OleDbDataReader _ command ExecuteReader Do While dataReader Read Console WriteLine vbTab & {0} & vbTab & {1} , _ dataReader 0 , dataReader 1 Loop dataReader Close Catch ex As Exception Console WriteLine ex Message End Try End Using End Sub Private Shared Function GetConnectionString As String ' To avoid storing the connection string in your code, ' you can retrieve it from a configuration file ' Assumes Northwind mdb is located in c Data folder Return Provider Microsoft Jet OLEDB 4.0;Data Source _ & c DataNorthwind mdb;User Id admin;Password ;
  24. 24. Just do it! • • OleDB Provider Northwind Ms Access Import Provider Imports System.Data.OleDB Dim objConn As New System.Data.OleDbConnection() objConn.ConnectionString = “Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:Northwind.mdb”
  25. 25. objConn.Open() objConn.Close()
  26. 26. Command Sql Select, Update, Delete, Insert Dim objComm As New OleDb.OleDbCommand() Provider CommandText objComm.CommandText = “Select * from Employyes” objComm.ExecuteNonQury objComm.ExecuteScalar objComm.ExecuteReader
  27. 27. Command Sql Select, Update, Delete, Insert Dim objComm As New OleDb.OleDbCommand() Provider CommandText objComm.CommandText = “Select * from Employyes” objComm.ExecuteNonQury objComm.ExecuteScalar objComm.ExecuteReader
  28. 28. objComm.ExecuteReader Set dataReader Dim dr As OleDb.OleDbDataReader dr = objComm.ExcuteReader Item DataReader While dr.read() Dim myObj As object = dr.Item(3) Dim myOtherObj As object = dr.Item(“Customer”) End While
  29. 29. objComm.ExecuteReader Set dataReader Dim dr As OleDb.OleDbDataReader dr = objComm.ExcuteReader Item DataReader While dr.read() Dim myObj As object = dr.Item(3) Dim myOtherObj As object = dr.Item(“Customer”) End While
  30. 30. DataReader Click ListBox1
  31. 31. Dim strSQL As String = “Select CustomerID,CompanyName F Sub Dim objConn As New OleDbConnection(StrConn) Dim objComm As New OleDbCommand(strSQL,objConn) Dim dr As OleDbDataReader objConn.Open() dr = objComm.ExecuteReader() ListBox1.Items.Add(“ ” & ControlChars.Tab & “ Do while dr.Read() Dim strRecord As String = dr.Item(“CustomerID”).tostring( Item(“CompanyName”).Tostring() ListBox1.Items.Add(strRecord) Loop
  32. 32. DataReader
  33. 33. DataAdapter DataAdapter => Dim da As New OleDb.OleDbDataAdapter Method Fill DataSet => dataAdapter.Fill(dataSet,tab Dim objConn As New Data.OleDbConnection(“Provider=Microsoft.Jet.OL _ EDB.4.0;Data Source=C:Northwind.mdb”) Dim strSQL As String = “Select * from Employees” Dim da As New OleDb.OleDbDataAdapter Da.SelectCommand = New OleDb.OleDbCommand(strSQL,objConn) Dim ds As New DataSet da.Fill(ds,”Employees”) dataSet dataSet.Tables(table).rows(row).(field) textbox1.text = ds.Tables(“employees”).row(0).(“FirstName”) ds.Tables(“employees”).row(0).(“FirstName”
  34. 34. DataAdapter Method Update => dataAdapter.Update(dataSet,table)
  35. 35. DataSet • DataSet Dim tb As DataTable = ds.Tables(“Employees”) Dim row As DataRow = tb.NewRow() row(“FirstName”) = “Chaimard” row(“LastName”) = “Kama” tb.Rows.Add(row)
  36. 36.  DataSet Dim tb As DataTable = ds.Tables(“Employees”) For Each row As DataRow In tb.Rows if row(“FirstName”) = “Chaimard” Then row.(“FirstName”) = “Superman” end if Next
  37. 37.  DataSet Dim tb As DataTable = ds.Tables(“Employees”) For Each row As DataRow In tb.Rows if row(“FirstName”) = “Chaimard” Then row.dalete() end if Next
  38. 38. Binding Data • Simple Binding Data textbox, Label • Complex Binding Data ListBox, ComboBox
  39. 39.  Simple Binding Data : object.DataBindings.Add(propertyName,dataSource,dataMember) : textbox1.dataBindings.Add(“text”,ds.Tables(“emp”), “FirstName”)  Listbox1.DataSource = ds.Tables(“emp”) Listbox1.DisplayMember = “FirstName” : Complex Binding Data With cboStudent ComboBox .datasource = ds.Tables(“Faculty”) .displaymember = “DescriptionThai” .ValueMember = “FacultyCode” .DataBindings.Add(“SelectValue”,objBS,“facultyCode”) End with 2. Student Student
  40. 40.  : me.bindingContext(ds.Tables.(“emp”)).position = 0 : me.bindingContext(ds.Tables.(“emp”)).position += 1 : me.bindingContext(ds.Tables.(“emp”)).position =1 : me.bindingContext(ds.Tables.(“emp”)).position = me.bindingContext(ds.Tables.(“emp”)).count - 1  BindingSource Dim objBS As New BindingSource objBS.DataSource = ds.Tables(“emp”) (ds.Tables(“emp”) emp ) : objBS.MoveFirst : objBS.MoveNext

×