Reading a Column Value Using Strongly Typed DataSet Classes
Số trang: 7
Loại file: pdf
Dung lượng: 42.77 KB
Lượt xem: 7
Lượt tải: 0
Xem trước 2 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Reading a Column Value Using Strongly Typed DataSet Classes A strongly typed DataSet object allows you read a column value using a property with the same name as the column.
Nội dung trích xuất từ tài liệu:
Reading a Column Value Using Strongly Typed DataSet ClassesReading a Column Value Using Strongly Typed DataSet ClassesA strongly typed DataSet object allows you read a column value using a property withthe same name as the column. For example, to read the CustomerID of a column, you canuse myDataRow.CustomerID rather than myDataRow[CustomerID]. This is a nicefeature because the compiler can then catch any errors in column spellings at compiletime rather than runtime. For example, if you incorrectly spelled CustomerID asCustimerID, then the mistake would be caught by the compiler.Another feature of a strongly typed DataSet is that when you work with it in VS .NET,IntelliSense automatically pops up the properties and methods of the DataSet when youare typing. You can then pick the property or method from the list, rather than have totype it all in.The downside to using a strongly typed DataSet is that you must do some initial work togenerate it before you can use it. If the columns in your database tables dont change veryoften, then you should consider using strongly typed DataSet objects. On the other hand,if your database tables change a lot, you should probably avoid them because youll needto regenerate the strongly typed DataSet to keep it synchronized with the definition of thedatabase table.Note Youll find a completed VS .NET example project for this section in the StronglyTypedDataSet directory. You can open this project in VS .NET by selecting File ➣ Open ➣ Project and opening the WindowsApplication4.csproj file. Youll need to change the ConnectionString property of the sqlConnection1 object to connect to your SQL Server Northwind database. You can also follow along with the instructions in this section by copying the DataReader directory to another directory and using that project as your starting point.Creating a Strongly Typed DataSet ClassIn this section, youll create a strongly typed DataSet class that is used to access theCustomers table. If youre following along with these instructions, open the DataReaderproject in VS .NET and double-click Form1.cs in the Solution Explorer window. Youopen the Solution Explorer window by selecting View ➣ Solution Explorer.Next, select File ➣ Add New Item. Select Data Set from the Templates area and enterMyDataSet.xsd, as shown in Figure 10.3.Figure 10.3: Adding a new Data SetClick the Open button to continue.VS .NET will add MyDataSet.xsd to your project, as shown in Figure 10.4.Figure 10.4: MyDataSet.xsdAt the bottom left of Figure 10.4, youll notice two tabs: DataSet and XML. The DataSettab is displayed by default and you use it to see the visual view of your DataSet. TheXML tab allows you to see the XML file of your DataSet.Next, make sure youve opened the Server Explorer window; to open the window, selectView ➣ Server Explorer. Open the Data Connections node and either use an existingconnection to your Northwind database or create a new one by right-clicking on the DataConnections node and selecting Add Connection from the pop-up menu.Double-click your connection and drill down to the table, view, or stored procedure youwant use, and then drag it onto your form. Go ahead and drag the Customers table ontoyour form. Figure 10.5 shows the form once the Customers table has been added.Figure 10.5: Customers table added to formNote You can add multiple tables to your form and define relations between them.Next, save your work by selecting File ➣ Save All or press Ctrl+S on your keyboard.Your project now contains a XSD file named MyDataSet.xsd, as shown in Listing 10.14.You can view this file by clicking the XML tab at the bottom of the XML Designerwindow.Listing 10.14: MYDATASET.XSD Notice that this file contains the details of the columns in the Customers table.Your project also contains a new class file named MyDataSet.cs, which contains yourstrongly typed DataSet class. You can view the contents of this file using the SolutionExplorer window. You open the Solution Explorer window by selecting View ➣Solution Explorer.Note To view the MyDataSet.cs file, click the Show All Files button in the Solution Explorer window.Next, expand the node beneath MyDataSet.xsd. Youll see MyDataSet.cs, as shown inFigure 10.6, and a file named MyDataSet.xsx, which contains layout information for thevisual view of your DataSet.Figure 10.6: Viewing all the files using the Solution Explorer windowGo ahead and open MyDataSet.cs by double-clicking it in the Solution Explorer window.View the code for this form by selecting View ➣ Code. One of the classes declared inthat file is MyDataSet. This class is derived from the DataSet class. Youll use it in thenext section to create a strongly typed DataSet object to access the Customers table.Using a Strongly Typed DataSet ClassOnce you have your strongly typed MyDataSet class, you can create an object of thatclass using the following code:MyDataSet myDataSet = new MyDataSet();You can also create a strongly typed DataTable table object using theMyDataSet.CustomersDataTable class and populate it with rows from the Customerstable. For example, you can set the Form1_Load() method of your form to retrieve theCustomerID, CompanyName, and Address column values from the Customers table andadd them to a ListView control named listView1. To do this, double-click Form1.cs inthe Solution Explorer windows, view the code, and set the Form1_Load() method asfollows:private void Form1_Load(object sender, System.EventArgs e){ System.Data.SqlClient.SqlCommand mySqlCommand = sqlConnection1.CreateCommand(); mySqlCommand.CommandText = SELECT CustomerID, CompanyName ...
Nội dung trích xuất từ tài liệu:
Reading a Column Value Using Strongly Typed DataSet ClassesReading a Column Value Using Strongly Typed DataSet ClassesA strongly typed DataSet object allows you read a column value using a property withthe same name as the column. For example, to read the CustomerID of a column, you canuse myDataRow.CustomerID rather than myDataRow[CustomerID]. This is a nicefeature because the compiler can then catch any errors in column spellings at compiletime rather than runtime. For example, if you incorrectly spelled CustomerID asCustimerID, then the mistake would be caught by the compiler.Another feature of a strongly typed DataSet is that when you work with it in VS .NET,IntelliSense automatically pops up the properties and methods of the DataSet when youare typing. You can then pick the property or method from the list, rather than have totype it all in.The downside to using a strongly typed DataSet is that you must do some initial work togenerate it before you can use it. If the columns in your database tables dont change veryoften, then you should consider using strongly typed DataSet objects. On the other hand,if your database tables change a lot, you should probably avoid them because youll needto regenerate the strongly typed DataSet to keep it synchronized with the definition of thedatabase table.Note Youll find a completed VS .NET example project for this section in the StronglyTypedDataSet directory. You can open this project in VS .NET by selecting File ➣ Open ➣ Project and opening the WindowsApplication4.csproj file. Youll need to change the ConnectionString property of the sqlConnection1 object to connect to your SQL Server Northwind database. You can also follow along with the instructions in this section by copying the DataReader directory to another directory and using that project as your starting point.Creating a Strongly Typed DataSet ClassIn this section, youll create a strongly typed DataSet class that is used to access theCustomers table. If youre following along with these instructions, open the DataReaderproject in VS .NET and double-click Form1.cs in the Solution Explorer window. Youopen the Solution Explorer window by selecting View ➣ Solution Explorer.Next, select File ➣ Add New Item. Select Data Set from the Templates area and enterMyDataSet.xsd, as shown in Figure 10.3.Figure 10.3: Adding a new Data SetClick the Open button to continue.VS .NET will add MyDataSet.xsd to your project, as shown in Figure 10.4.Figure 10.4: MyDataSet.xsdAt the bottom left of Figure 10.4, youll notice two tabs: DataSet and XML. The DataSettab is displayed by default and you use it to see the visual view of your DataSet. TheXML tab allows you to see the XML file of your DataSet.Next, make sure youve opened the Server Explorer window; to open the window, selectView ➣ Server Explorer. Open the Data Connections node and either use an existingconnection to your Northwind database or create a new one by right-clicking on the DataConnections node and selecting Add Connection from the pop-up menu.Double-click your connection and drill down to the table, view, or stored procedure youwant use, and then drag it onto your form. Go ahead and drag the Customers table ontoyour form. Figure 10.5 shows the form once the Customers table has been added.Figure 10.5: Customers table added to formNote You can add multiple tables to your form and define relations between them.Next, save your work by selecting File ➣ Save All or press Ctrl+S on your keyboard.Your project now contains a XSD file named MyDataSet.xsd, as shown in Listing 10.14.You can view this file by clicking the XML tab at the bottom of the XML Designerwindow.Listing 10.14: MYDATASET.XSD Notice that this file contains the details of the columns in the Customers table.Your project also contains a new class file named MyDataSet.cs, which contains yourstrongly typed DataSet class. You can view the contents of this file using the SolutionExplorer window. You open the Solution Explorer window by selecting View ➣Solution Explorer.Note To view the MyDataSet.cs file, click the Show All Files button in the Solution Explorer window.Next, expand the node beneath MyDataSet.xsd. Youll see MyDataSet.cs, as shown inFigure 10.6, and a file named MyDataSet.xsx, which contains layout information for thevisual view of your DataSet.Figure 10.6: Viewing all the files using the Solution Explorer windowGo ahead and open MyDataSet.cs by double-clicking it in the Solution Explorer window.View the code for this form by selecting View ➣ Code. One of the classes declared inthat file is MyDataSet. This class is derived from the DataSet class. Youll use it in thenext section to create a strongly typed DataSet object to access the Customers table.Using a Strongly Typed DataSet ClassOnce you have your strongly typed MyDataSet class, you can create an object of thatclass using the following code:MyDataSet myDataSet = new MyDataSet();You can also create a strongly typed DataTable table object using theMyDataSet.CustomersDataTable class and populate it with rows from the Customerstable. For example, you can set the Form1_Load() method of your form to retrieve theCustomerID, CompanyName, and Address column values from the Customers table andadd them to a ListView control named listView1. To do this, double-click Form1.cs inthe Solution Explorer windows, view the code, and set the Form1_Load() method asfollows:private void Form1_Load(object sender, System.EventArgs e){ System.Data.SqlClient.SqlCommand mySqlCommand = sqlConnection1.CreateCommand(); mySqlCommand.CommandText = SELECT CustomerID, CompanyName ...
Tìm kiếm theo từ khóa liên quan:
kĩ thuật lập trình công nghệ thông tin lập trình ngôn ngữ lập trình C Shark C# sybex - c.sharp database programming Reading a Column Value Using Strongly Typed DataSet ClassesTài liệu có liên quan:
-
52 trang 468 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 367 0 0 -
96 trang 334 0 0
-
74 trang 329 0 0
-
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 321 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 320 1 0 -
Giáo trình Lập trình hướng đối tượng: Phần 2
154 trang 316 0 0 -
Kỹ thuật lập trình trên Visual Basic 2005
148 trang 310 0 0 -
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 304 0 0 -
Tài liệu hướng dẫn sử dụng thư điện tử tài nguyên và môi trường
72 trang 302 0 0