tailieunhanh - Using ADO.NET Programmatically
Sử dụng lập trình Trong tập tiếp theo của bài tập, bạn sẽ viết mã của riêng bạn để truy cập cơ sở dữ liệu hơn là kéo bảng từ cửa sổ các nguồn dữ liệu. | Using Programmatically In the next set of exercises you will write your own code to access the database rather than dragging tables from the Data Sources window. The aim of the exercise is to help you learn more about and understand the object model implemented by by programming it manually. In many cases this is what you will have to do in real life the drag-and-drop approach is fine for creating prototypes but on many occasions you will want more control over how data is retrieved and manipulated. The application you are going to create will generate a simple report displaying information about customers orders. The program will prompt the user for a CustomerlD and then display the orders for that customer. Connect to the database 1. Create a new project called ReportOrders by using the Console Application template. Save it in the Microsoft Press Visual CSharp Step By Step Chapter 23 folder in your My Documents folder. Click OK. 2. In the Solution Explorer change the name of to . Notice that the name of the Program class in the Code and Text Editor window changes to Report automatically. 3. In the Code And Text Editor window add the following statement under the using statement using The namespace contains the specialized classes used to gain access to SQL Server. 4. Locate the Main method of the Report class. Add the following statement that declares a SqlConnection object SqlConnection dataConnection new SqlConnection SqlConnection is a subclass of the Connection class. It is designed to handle connections to SQL Server databases only. 5. After the variable declaration add a try catch block to the Main method. All the code that you will write for gaining access to the database goes inside the try part of this block remember that you must be prepared to handle exceptions whenever you use a database. 6. try 7. 8. You will add your code here in a .
đang nạp các trang xem trước