[ C# - ADO.Net ] Giới thiệu tóm tắt các đối tượng để kết nối dữ liệu (ADO.Net) trong C#


I. Đối tượng Sqlconnection  [using System.Data.SqlClient ;]

1. Kết nối theo đặc quyền hệ điều hành

Cú pháp 1 : 
Server = servername [ \InstanceName ];
Database = databasename;
Integrated Security = SSPI;

Cú pháp 2 :
Server = servername[ \InstanceName ];
Database = databasename;
Integrated Security = true;

Cú pháp 3 :
Server = servername [ \InstanceName ];
Initial Catolog = databasename;
Integrated Security = true;

       Chú ý :  Tài khoản đăng nhập phải được khai báo trong phần Login

2. Kết nối theo đặc quyền SQL Server

Cú pháp 1: 
Server = Servername[ \InstanceName ];
Database = Databasename;
Use ID = Username;
Password = YourPasword;
[Connection Timeout = second;]
[port = portno;]
[Persist Security Info = true;]

      Chú ý : Servername có thể là : (local) hoặc ( . ) hoặc (Địa chỉ IP) .

Cú pháp 2 : Với dạng Attachment và phiên bản SQL Server 2005 (SQLEXPRESS)

@”Data Source = (local)\SQLEXPRESS; AttachDbFilename = <Đường dẫn tới file Database>; Integrated Security = true; Use Instance = true”;

3. Tập tin lưu chuỗi kết nối
Ta có thể sử dụng các định dạng *.ini hoặc *.txt để lưu chuỗi kết nối hoặc lưu chuỗi kết nối ngay trong class hay form. Tuy nhiên khi làm việc với .Net chúng ta nên sử dụng định dạng *.config đã được hố trợ sẵn.

Cú pháp 1:
?xml version="1.0" encoding="utf-8" ?>
<configuration>  <connectionStrings>    <add name="SqlServer"         connectionString=          " Server = servername[ \InstanceName ]; Database = databasename; Integrated Security = true; "          providerName ="System.Data.SqlClient"/>  </connectionStrings></configuration>

 - Cách đọc nội dung chuỗi kết nối theo cú pháp 1 ta sử dụng phương thức connectionStrings của lớp connectionStringSettings thuộc không gian tên System.Configuration;

   *
*     *

II. Đối tượng SQLCommand

1. Khai báo
SqlCommand  sqlCommand;

2. Khởi tạo
Có 4 kiểm khởi tạo để khai báo khởi tạo đối tượng này
  + sqlCommand = new SqlCommand();
  + sqlCommand = new SqlCommand(string CommandText);
  + sqlCommand = new SqlCommand(string CommandText, SqlConnection   sqlConnection);
  + sqlCommand = new SqlCommand(string CommandText, SqlConnection   sqlConnection, SqlTrasaction sqlTransaction);

3. Các thuộc tính
  + CommandText: Cho phép khai báo một chuỗi phát biểu SQL Server
  
VD : 
 String strSQL = “Select * from TBLSinhvien”; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL;

  + CommandType
Cho phép ta chọn một trong 3  giá trị enum là : Text,TableDirect,StoredProcedure
VD:
String strSQL = “spDanhsachSV”; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL;am sqlCommand.CommandType = CommandType.StoredProcedure;

Lưu ý: khi thủ tục có tham số truyền vào thì ta có thể sử dụng đối tượng SqlParameterCollection hay SqlParameter

+ CommandTimeout
Cho phép khai báo thời gian chờ thực thi phát biểu SQL được tính bằng    giây 

VD : 
 String strSQL = “spDanhsachSV”; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.CommandTimeout = 30;

 + Connection
Cho phép ta khởi tạo đối tượng sqlConnection mà không cần phải thông  qua Constructor 

 VD : 
 String strSQL = “spDanhsachSV”; sqlCommand = new SqlCommand(); sqlCommand.CommandText = strSQL; sqlCommand.Connection = sqlConnection; sqlCommand.CommandType = CommandType.StoredProcedure; sqlCommand.CommandTimeout = 30;

4. Phương thức

+ Phương thức ExecuteNonQuery
   Thực thi các phát biểu SQL, thử tục nội tại, và nó trả về số bản ghi đựoc thực thi

VD :
sqlCommand.Connection = sqlConnection; sqlConnnection.Open();  String strSQL = “delete from TBLSinhvien where Masv = ‘SV0000001’”;  sqlCommand = new SqlCommand();  sqlCommand.CommandText = strSQL;  int records = sqlCommand.ExcuteNonQuery(); sqlConnection.Close(); sqlConnection.Dispose();

+ Phương thức ExecuteScalar
      Phương thức này thực thi phát biểu SQL Server giá trị trả về là kiểu đối tượng (object)
Nó thường được dùng để lấy giá trị của tổng các mấu tin hay giá trị của cột hay hàng thứ nhất

+ Phương thức ExecuteReader
      Khác với hai phương thức trên , phương thức này trả về tập các giá trị chỉ đọc một chiều và dùng đối tượng  sqlDataReader để nắm dữ tập dữ liệu đó.

 +  Phương thức ExcuteXmlReader
      Phương thức này tương tự như phương thức ExecuteReader nhưng nó trả về tập dữ liệu có định dạng XML và sử dụng đối tượng XMLReader để nắm dữ tập dữ liệu đó.

5. Xây dựng lớp dùng chung cho SQLServer
      Khi chúng ta muốn sử dụng các phương thức trên chung trong cơ sở dữ liệu SQL Server ,để có thể truyền vào tham số thì chúng ta sẽ xây dụng lớp dùng chung , giả sử là Database và kêt hợp với đôí tượng SqlConnection ở phần trên .

--------------------------------------------------------------------
Clip ví dụ về kết nối cơ sở dữ liệu với C# [đầy đủ - dễ hiểu]


[Nguôn: Kenhsinhvien]

Tìm kiếm nội dung khác: