12/20/13

[ADO.NET] Sử dụng SQL - Transaction với C#

Giả sử bạn có bảng sản phẩm với các thông tin như sau:
  1. create table Product  
  2. (  
  3. ProductID     int not null primary key identity,  
  4. ProductName varchar (50) not null,  
  5. Description varchar(1000) not null,  
  6. Price money  
  7. )  

Thủ tục sử dụng để lưu thông tin một sản phẩm vào cơ sở dữ  liệu có thể viết theo một trong hai cách sau:
  1. create procedure spInsertProduct  
  2. (  
  3. -- Khai báo biến, biến này phải có kiểu dữ liệu giống như trong CSDL  
  4. @name varchar(50),      
  5. @description varchar(1000),  
  6. @price money  
  7. -- Biến trả về kết quả thực thi  
  8. @result int output  
  9. )   
  10. as  
  11.       begin transaction  
  12.              insert into Product (ProductName, Description, Price ) values(@name,@description,@price)  
  13.       if @@ERROR <> 0  
  14.     begin               
  15.         -- Có lỗi xảy ra, rollback transaction  
  16.         set @result=0  
  17.         ROLLBACK  
  18.     end  
  19.       else  
  20.     begin  
  21.         --  Thành công! commit the transaction  
  22.         set @result=1  
  23.         COMMIT  
  24.     end  


hoặc

  1. create procedure spInsertProduct  
  2. (  
  3. -- Khai báo biến, biến này phải có kiểu dữ liệu giống như trong CSDL  
  4. @name varchar(50),      
  5. @description varchar(1000),  
  6. @price money  
  7. -- Biến trả về kết quả thực thi  
  8. @result int output  
  9. )   
  10. as  
  11.       begin try  
  12.     begin transaction  
  13.         insert into Product (ProductName, Description, Price ) values  
  14.   
  15. (@name,@description,@price)  
  16.         --Nếu chạy đến câu lệnh tiếp theo --> thực thi thành công  
  17.         set @result=1  
  18.         COMMIT  
  19.       end try  
  20.   
  21.       begin catch  
  22.       
  23.           if @@TRANCOUNT > 0  
  24.         begin               
  25.             -- Có lỗi xảy ra, rollback transaction  
  26.             set @result=0  
  27.             ROLLBACK  
  28.         end  
  29.      end catch  


Trong phần mã C# ứng dụng, bạn thực hiện gọi thực thi thủ tục và kiểm tra kết quả trả về:




  1. // strConn is your connection string   
  2. SqlConnection cn = new SqlConnection(strConn);  
  3. SqlCommand cmd = cn.CreateCommand();  
  4. cmd.CommandText = "spInsertProduct";  
  5. cmd.CommandType = CommandType.StoredProcedure;  
  6.   
  7. cmd.Parameters.Add("@name", SqlDbType.VarChar, 50);  
  8. cmd.Parameters["@name"].Direction = ParameterDirection.Input;  
  9. cmd.Parameters["@name"].Value = txtName.Text;  
  10.   
  11. cmd.Parameters.Add("@description", SqlDbType.VarChar, 1000);  
  12. cmd.Parameters["@description"].Direction = ParameterDirection.Input;  
  13. cmd.Parameters["@description"].Value = txtDescription.Text;  
  14.   
  15. cmd.Parameters.Add("@result", SqlDbType.Int);  
  16. cmd.Parameters["@result"].Direction = ParameterDirection.ReturnValue;  
  17. cn.Open();  
  18. cmd.ExecuteNonQuery();  
  19. cn.Close();  
  20. int result = Convert.ToInt32(cmd.Parameters["@result"].Value);  
  21. // kiểm tra giá trị trả về  
  22. if(result==1)  
  23. MessageBox.Show("Success");   
  24. else  
  25. MessageBox.Show("Error");   















No comments: