Bản thuyết trình đang được tải. Xin vui lòng chờ

Bản thuyết trình đang được tải. Xin vui lòng chờ

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

Các bản thuyết trình tương tự


Bản thuyết trình với chủ đề: "Chương 5 KẾT NỐI ĐẾN DATA SOURCE."— Bản ghi của bản thuyết trình:

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

2 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

3 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

4 .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?

5 .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

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

7 .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 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

8 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

9 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

10 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;…

11 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"

12 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”

13 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

14 Đố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

15 Đố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;

16 Đố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

17 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

18 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

19 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

20 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(); }

21 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 or 18488, this indicates that the specified password has expired or must be reset. See the ChangePassword method for more information.

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

23 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

24 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";

25 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

26 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>

27 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;

28 Tóm tắt chương 5


Tải xuống ppt "Chương 5 KẾT NỐI ĐẾN DATA SOURCE."

Các bản thuyết trình tương tự


Quảng cáo bởi Google