Chương 5 KẾT NỐI ĐẾN DATA SOURCE.

Slides:



Advertisements
Các bản thuyết trình tương tự
Công nghệ phần mềm Các quy trình phần mềm.
Advertisements

CHÚA NHẬT 4 MÙA THƯỜNG NIÊN
CHÚA NHẬT 33 MÙA THƯỜNG NIÊN NĂM B
LỄ ĐỨC MARIA LÀ MẸ THIÊN CHÚA
Các nguyên tắc làm việc với ASP
VIETNAM BUỔI HỌP TỔ CÔNG TÁC May 23, 2019
Lễ Chúa Giêsu Lên Trời Năm C
PHÒNG TÀI NGUYÊN VÀ MÔI TRƯỜNG CÁC CÔNG TÁC BẢO VỆ MÔI TRƯỜNG
Hệ điều hành Windows Sever 2003
CHÚA NHẬT 29 MÙA THƯỜNG NIÊN
“Khi Chúa Giêsu đã chịu phép rửa và đang cầu nguyện, thì trời mở ra”.
ASP Giáo viên: TS. Trương Diệu Linh Bộ môn Truyền thông & Mạng
BIDV Czech Rep. Representative Office
CHALLENGES & OPPORTUNITIES
Kiểm thử và đảm bảo chất lượng phần mềm
Hùng Phương & Thanh Quảng thực hiện
Chương 3 Exception.
Xinh đẹp! Beautiful!.
TWENTY-FIFTH SUNDAY IN ORDINARY TIME
CHÚA NHẬT 19 MÙA THƯỜNG NIÊN
Chúa Nhật 13 Thường Niên Năm C Hùng Phương & Thanh Quảng thực hiện
Dạy những học viên đọc kém Teaching Students with Low Reading Levels
Thực hành 5S trong văn phòng
Đồ họa máy tính Ánh sáng Bùi Thế Duy - Bộ môn KHMT 7/15/2019.
Sáng tác: MS David Dong 2007.
Chương 3 – Các kỹ thuật gỡ lỗi và kiểm thử chương trình
VÒNG 1 - BẢNG A Đại học Giao thông Vận tải 2 Học viện Kỹ thuật Mật mã
Chương 7 INTERFACE.
THE HOLY FAMILY OF JESUS, MARY AND JOSEPH
Hướng dẫn sử dụng Tôn Nữ Phương Mai Tháng 3/2013.
TRƯỜNG ĐH CÔNG NGHỆ SÀI GÒN
Lương Trần Hy Hiến - LINQ to SQL Lương Trần Hy Hiến -
Bài 3: Ngôn ngữ lập trình C#
Chương 02 CẤU TRÚC VÀ CÁC THÀNH PHẦN CHƯƠNG TRÌNH C/C++
Chương 5 ỨNG DỤNG CSDL VÀ ADO.NET.
CHƯƠNG II: CHUẨN ETHERNET
Bài 1: giới thiệu C# và .NET framework
Hiểu Biết về Kết Quả SBAC CAASPP – California Assessment of Student Performance and Progress (Đánh Giá của California về Thành Quả và Tiến.
CHÚA NHẬT 5 MÙA THƯỜNG NIÊN
17th Sunday in Ordinary Time - Year C Chúa Nhật 17 Thường Niên Năm C
THE NATIVITY OF THE LORD CHRISTMAS
YÊU CẦU ĐẶT RA Tôi có một số đoạn phim / hình ảnh (vừa quay / chụp được hoặc sưu tầm được,…), tôi muốn lắp ghép nó để trở thành một đoạn phim trình.
Hệ thống quản lý và phát hành kho quỹ theo hướng tập trung - CMO
Maria thì ghi nhớ tất cả những việc đó và suy niệm trong lòng
PALM SUNDAY OF THE LORD’S PASSION
Thiết kế giao diện người dùng
Korea-Vietnam Friendship IT College
Tin Mừng Chúa Giêsu Kitô theo Thánh Matthêu.
Chuyên đề : Web Design Bộ môn KTMT – Viện CNTT&TT
Cài đặt và thiết lập DNS cho Domain
LỄ ĐỨC MARIA, MẸ THIÊN CHÚA MARY, THE HOLY MOTHER OF GOD
Trainer: Bạch Ngọc Toàn Phát hành bởi: TEDU –
SẮP XẾP, TÌM KIẾM, LỌC DỮ LIỆU
MÔN HỌC: NHẬP MÔN VỄ KỸ THUẬT CHƯƠNG 7: KỸ NĂNG GIẢI QUYẾT VẤN ĐỀ
Mô hình OSI và TCP/IP MẠNG MÁY TÍNH.
CHÚA NHẬT LỄ LÁ NĂM C PALM SUNDAY
Nhập môn Công nghệ học Phần mềm Introduction to Software Engineering
LỄ THĂNG THIÊN THE ASCENSION OF THE LORD.
Wireless Communications Principles and Practice
TRƯỜNG ĐẠI HỌC KINH TẾ QUỐC DÂN
Mục tiêu Ưu điểm của ADO.NET Kiến trúc của ADO.NET
HỆ QUẢN TRỊ CƠ SỞ DỮ LIỆU SQL SERVER 2012
CHÚA NHẬT 13 MÙA THƯỜNG NIÊN THIRTEENTH SUNDAY IN ORDINARY TIME
CUỘC SỐNG ĐẸP THAY Âm nhạc: Ernesto Cortazar, “La Vida Es Bella”
HƯỚNG DẪN KÍCH HOẠT TÀI KHOẢN
Nhịp & Phách (Time and Beat).
Hàm Lập trình nâng cao.
Bản ghi của bản thuyết trình:

Chương 5 KẾT NỐI ĐẾN DATA SOURCE

Nội dung Các bước kết nối Data Source .NET Data Provider Chuỗi kết nối – Connection String Đối tượng Connection Quản lý Connection Bắt ngoại lệ của Connection Connection Pooling Lưu trữ chuỗi kết nối

Các bước kết nối Data Source 5 bước cơ bản Bước 1: Chọn .Net Data Provider Bước 2: Tạo đối tượng kết nối Bước 3: Mở kết nối Bước 4: Kiểm tra trạng thái của kết nối Bước 5: Đóng kết nối

.NET Data Provider Khái niệm .NET Data Provider Các lớp trong .NET Data Provider Nên dùng .NET Data Provider nào?

.NET Data Providers Khái niệm .NET Data Provider Định nghĩa .NET Data Provider Xem lại Chương 2 Các loại .Net Data Provider Tên Provider Provider Namespace SQL Server .NET Data Provider System.Data.SqlClient Oracle .NET Data Provider System.Data.OracleClient OLE DB .NET Data Provider System.Data.OleDb ODBC .NET Data Provider System.Data.ODBC XML trong SQL Server System.Data.SqlXml … SQL Server .NET Data Provider OLE DB .NET Data Provider ODBC .NET Data Provider

.NET Data Providers Các lớp trong .NET Data Provider xxxConnection – SqlConnection xxxTransaction – SqlTransaction xxxCommand – SqlCommand xxxParameter – SqlParamater xxxDataReader – SqlDataAdapter xxxDataAdapter – SqlDataAdapter

.NET Data Providers Nên dùng .NET Data Provider nào? Tên Data Source Provider Namespace MS SQL Server 7.0 trở lên SQL Server .NET Data Provider Oracle 8.1.6 trở lên Oracle .NET Data Provider ODBC ODBC .NET Data Provider OleDb OleDB .NET Data Provider XML trong SQL Server XML .NET Data Provider SQL Server .NET Data Provider OLE DB .NET Data Provider ODBC .NET Data Provider

Chuỗi kết nối Bảo mật CSDL Khái niệm chuỗi kết nối Chuỗi kết nối cho OleDB (MS Access) và MS SQL Server Bảng từ khóa ConnectionStringBuilder

Chuỗi kết nối Bảo mật CSDL Bảo mật CSDL SQL Server Windows Authentication Mixed Mode SQL Server Authentication

Chuỗi kết nối Khái niệm chuỗi kết nối Để mở kết nối, chúng ta cần chỉ ra một số thông tin (tham số): tên server, tên csdl, user ID, password, … Mỗi data source cần một tập các thông tin khác nhau Định nghĩa Chuỗi kết nối (Connection String): Là 1 tập các thông tin cung cấp cho một thiết lập kết nối (tham số), và có hình thức các cặp key-value cách nhau bằng dấu “;” Một cách uyển chuyển để cung cấp thông tin cho một thiết lập kết nối là dùng một chuỗi kế nối parameterName1=value1; parameterName2=value2;…

Chuỗi kết nối Chuỗi kết nối cho OleDB và MS SQL Server Chuỗi kết nối cho CSDL OleDB (MS Access) "Provider=Microsoft.Jet.OleDb.4.0; Data Source=DuLieu.mdb" Chuỗi kết nối cho CSDL MS SQL Server "Server=tenServer; Database=tenCSDL; User Id=tenUser; Password=tenPass" Nếu không chỉ đường dẫn tuyệt đối thi ADO.NET tìm csdl trong đường dẫn của ứng dụng SSPI stands for the Security Support Provider Interface "Server=local; Database=pubs; Integrated Security=SSPI"

Chuỗi kết nối Bảng từ khóa Một số từ khóa liên quan đến chuỗi kết nối cho SQL Server Keyword Ý nghĩa Data Source, Server, addr, address, network address Tên hay địa chỉ của database server. Initial Catalog, Database Tên của database Integrated Security, trusted_connection Bảo mật dựa trên HĐH Windows. Có thể gán bằng true, false, hay sspi. Default là false. User ID, uid, user user name để kết nối đến SQL Server khi không dùng trusted connection. Password, pwd password để kết nối đến SQL Server khi không dùng trusted connection. Pooling Khi bằng true, dùng pool để lưu trữ các connection. Default là true Max Pool Size Số connection tối đa được lưu trong connection pool. Default là 100. Min Pool Size Số connection tối thiểu được lưu trong connection pool. Default là 0. Connect Timeout, connection timeout, timeout Thời gian chờ (tính bằng giây) khi kết nối đến data store. Default là 15 giây. Keyword Description Data Source, addr, address, network address, server The name or IP address of the database server. Failover Partner Provides support for database mirroring in SQL Server 2005. AttachDbFilename, extended properties, initial filename The full or relative path and name of a file containing the database to be attached to. The path supports the keyword string |DataDirectory|, which points to the application's data directory. The database must reside on a local drive. The log filename must be in the format <database-File-Name>_log.ldf or it will not be found. If the log file is not found, a new log file is created. Initial Catalog, database The name of the database to use. Integrated Security, trusted_connection Used to connect to SQL Server using a secure connection, where authentication is through the user's domain account. Can be set to true, false, or sspi. The default is false. Persist Security Info, persistsecurityinfo If set to true, retrieving the connection string returns the complete connection string that was originally provided. If set to false, the connection string contains the information that was originally provided, minus the security information. The default is false. User ID, uid, user The user name to use to connect to the SQL Server when not using a trusted connection. Password, pwd The password to use to log in to SQL Server when not using a trusted connection. Enlist When set to true, the pooler automatically enlists the connection into the caller thread's ongoing transaction context. Pooling When set to true, causes the request for a new connection to be drawn from the pool. If the pool does not exist, it is created. Max Pool Size Specifies the maximum allowed connections in the connection pool. The default is 100. Min Pool Size Specifies the minimum number of connections to keep in the pool. The default is 0. Asynchronous Processing, async When set to true, enables execution of asynchronous commands on the connection. Synchronous commands should use a different connection, to minimize resource usage. The default is false. Connection Reset Indicates that the database connection will be reset when the connection is removed from the pool. The default is true. A setting of false results in fewer round-trips to the server when creating a connection, but the connection state is not updated. MultipleActiveResultSets When set to true, allows for the retrieval of multiple forward-only, read-only result sets on the same connection. The default is false. Replication Used by SQL Server for replication. Connect Timeout, connection timeout, timeout The time in seconds to wait while an attempt is made to connect to the data store. The default is 15 seconds. Encrypt If Encrypt is set to true and SQL Server has a certificate installed, all communication between the client and server is SSL encrypted. Load Balance Timeout, connection lifetime The maximum time in seconds that a pooled connection should live. The maximum time is checked only when the connection is returned to the pool. This setting is useful in load-balanced cluster configurations to force a balance between a server that is on line and a server that has just started. The default is 0. Network Library, net, network The network library DLL to use when connecting to SQL Server. Allowed libraries include dbmssocn (TCP/IP), dbnmpntw (Named Pipes), dbmsrpcn (Multiprotocol), dbmsadsn (AppleTalk), dbmsgnet (VIA), dbmsipcn (Shared Memory), and dbmsspxn (IPX/SPX). The default is dbmssocn (TCP/IP), but if a network is not specified and either "." or "(local)" is specified for the server, shared memory is used as the default. Packet Size The size in bytes for each packet that is sent to SQL Server. The default is 8192. Application Name, app The name of the application. If not set, this defaults to .NET SQL Client Data Provider. Current Language, language The SQL Server language record name. Workstation ID, wsid The name of the client computer that is connecting to SQL Server. Chú ý: 1. Rieng MS Access 2003 Provider = “Microsoft.jet.oledb.4.0; data source=tenfile.mdb” Data source lúc này không tương đương với server, addr, … (Nghĩa là chỉ dùng data source chỉ ra file csdl ms access) 2. Rieng MS Access 2007 Provider=“Microsoft.ace.oledb.12; data source=tenfile.accdb”

Chuỗi kết nối ConnectionStringBuilder ConnecitionsStringBuilder cho CSDL OleDB (MS Access) OleDbConnectionStringBuilder Provider DataSource ConnecitionsStringBuilder cho CSDL MS SQL Server SqlConnectionStringBuilder InitialCatalog UserID Password IntegratedSecurity

Đối tượng Connection Tạo đối tượng kết nối Kết hợp đối tượng kết nối với chuỗi kết nối Dùng Constructor của lớp Connection

Đối tượng Connection Tạo một đối tượng của lớp Connection (tương ứng với data source cho trước) SqlConnection conn = new SqlConnection(); Chỉ ra các tham số kết nối dùng property ConnectionString Cach 1 Tạo đối tượng kết nối để chuẩn bị mở kết nối SSPI = Security Support Provider Interface string strConn = "Data Source=(local); Initial Catalog=demoDB; Integrated Security=SSPI"; conn.ConnectionString=strConn;

Đối tượng Connection Có thể dùng constructor của lớp Connection string strConn = "Data Source=(local); Initial Catalog=demoDB; Integrated Security=SSPI"; SqlConnection conn = new SqlConnection(strConn); Cach 2

Quản lý Connection Mở kết nối Đóng kết nối Kiểm tra trạng thái đối tượng Connection

Quản lý Connection Mở kết nối Phương thức Open() Một số bước được thực hiện khi mở kết nối MỚI Thiết lập kênh vật lý (socket hay pipe) Bắt tay với Server Phân tích chuỗi kết nối Xác thực với Server … tốn thời gian Mo cang tre cang tot Dong cang som cang tot

Quản lý Connection Đóng kết nối Phương thức Close() Đặt connection đã mở vào connection pooler Phương thức Dispose() Đặt connection vào connection pooler Giải phóng các tài nguyên (Cho phép GC dọn dẹp) Tot nhat: Dispose Tot: Close Te nhat: Không close và dispose

Quản lý Connection Kiểm tra trạng thái đối tượng Connection Property State ConnectionState.Open ConnectionState.Closed SqlConnection conn = new SqlConnection(strConn); … if (conn.State == ConnectionState.Open) { conn.Close(); }

Bắt ngoại lệ của Connection string strConn = "Data Source=(local); Initial Catalog=demoDB; Integrated Security=SSPI"; SqlConnection conn = new SqlConnection(strConn); try { conn.Open(); //… } catch (SqlException ex) { //… } finally { conn.Dispose(); } Ném ra 2 ngoại lệ InvalidOperationException Cannot open a connection without specifying a data source or server.or The connection is already open. SqlException A connection-level error occurred while opening the connection. If the Number property contains the value 18487 or 18488, this indicates that the specified password has expired or must be reset. See the ChangePassword method for more information.

Connection Pooling Khái niệm Connection Pooling Điều khiển OleDb Connection Pooling Điều khiển SQL Server Connection Pooling

Connection Pooling Khái niệm Connection Pooling Định nghĩa “Connection Pooler”: Là bộ nhớ lưu trữ các kết nối CSDL vật lý đã mở Định nghĩa “Connection Pooling”: Là kỹ thuật tối ưu bằng cách lưu các connection đã mở vào trong pooler và tái sử dụng connection trong pooler khi cần thiết Khi có một yêu cầu kết nối đến CSDL và yêu cầu kết nối đó phù hợp với 1 kết nối trong connection pooler thì nó lấy kết nối đó ra sử dụng lại Phu hop connection string

Connection Pooling Điều khiển OleDb Connection Pool Mặc nhiên: dùng connection pooling Để bỏ OleDB Connection Pooling string strConn = "Provider = ; Data Source= ; Initial Catalog=demoDB; OLE DB Services= -4";

Connection Pooling Điều khiển SQL Server Connection Pool Mặc nhiên: dùng connection pooling Các tham số connection string cho connection pooling Connection Lifetime Connection Reset Enlist Max Pool Size Min Pool Size Pooling Pooling = false: bỏ pool Viết ví du minh họa: Đo thoi gian chay pool và không pool

Lưu trữ chuỗi kết nối Lưu trong file cấu hình: app.config .Net 1.x <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="ConnectionString" value="..."/> </appSettings> </configuration> .Net 2.0 <?xml version="1.0" encoding="utf-8" ?> <configuration> <connectionStrings> <add name="tên" connectionString="..."/> </connectionStrings> </configuration>

Lưu trữ chuỗi kết nối Đọc chuỗi kết nối từ file app.config References: System.configuration Namespace: using System.Configuration; Sử dụng lớp: ConfigurationManager string strConn; strConn = ConfigurationManager.ConnectionStrings["ten"].ConnectionString;

Tóm tắt chương 5