TAILIEUCHUNG - Beginning C# 2008 Databases From Novice to Professional phần 9

Khởi động chương trình bằng cách nhấn Ctrl + F5, và sau đó nhấp vào cơ sở dữ liệu ngoại lệ-2 nút. Bạn sẽ thấy hộp thông báo trong hình 16-9. Nhấn OK để đóng hộp thông báo, nhấn OK để đóng tiếp theo, và sau đó đóng cửa sổ. Tuy nhiên, kể từ khi cột EmployeeID trong bảng nhân viên là một cột IDENTITY, bạn có thể không rõ ràng chỉ định một giá trị cho nó. | CHAPTER 16 HANDLING EXCEPTIONS 387 3. Run the program by pressing Ctrl F5 and then click the Database Exception-2 button. You ll see the message box in Figure 16-9. Click OK to close the message box click OK to close the next one and then close the window. Figure 16-9. Stored procedure database exception message How It Works The stored procedure tries to insert a new employee into the Employees table. insert into employees employeeid firstname values 50 Cinderella However since the EmployeeID column in the Employees table is an IDENTITY column you can t explicitly assign a value to it. Tip Actually you can as the message indicates if you use SET IDENTITY_INSERT employees OFF in the stored procedure before you attempt the INSERT. This would allow you to insert explicit EmployeelD values but this seldom is or should be done. When this SQL error occurs the specific SqlException catch clause traps it and displays the information. The finally block then closes the connection. It s possible for stored procedures to encounter several errors. You can trap and debug these using the SqlException object as you ll see next. 388 CHAPTER 16 HANDLING EXCEPTIONS Try It Out Handling a Database Exception Part 3 Errors Collection The SqlException class SqlException class has an Errors collection property. Each item in the Errors collection is an object of type SqlError. When a database exception occurs the Errors collection is populated. For the example you ll try to establish a connection to a nonexistent database and investigate the SqlException s Errors collection. 1. Insert the code in Listing 16-6 into the button5_Click method. Note that you re intentionally misspelling the database name. Listing 16-6. button5_Click create connection SqlConnection conn new SqlConnection @ data source . sqlexpress integrated security true database northwnd create command SqlCommand cmd specify stored procedure to be executed .

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.