- Get link
- X
- Other Apps
Posted by
Real
- Get link
- X
- Other Apps
Although you can create the typed DataSet schema manually by adding a new DataSet object to the project (on the Project menu, click Add New Item, and then click DataSet), this example uses Server Explorer to build the DataSet schema automatically.
Add the DataRelation
Fill the Typed DataSet
This section adds code to fill the typed DataSet and to map the table names. This section also uses the DataGrid control to display the filled DataSet.
1.In Visual Studio .NET, on the File menu, point to New, and then click Project.
2.Click Visual Basic Projects under Project Types, and then click Windows Application under Templates.
3.On the View menu, click Server Explorer.
4.In Server Explorer, click Connect to Database, and then connect to your SQL ServerNorthwind database.
5.Drag the Customers and the Orders tables from Server Explorer to the current project. Notice that a SqlConnection object and two SqlDataAdapter objects are added to the project.
6.In the Properties window, click Generate Dataset, and then add both tables to theDataSet. Notice that an .xsd file is added to the project. The .xsd file is named according to the name that you chose for the DataSet class.
Add the DataRelation
1.In Solution Explorer, double-click the .xsd file that you created in the previous section.
2.Right-click the Customers table in the designer, point to Add, and then click New Relation.
3.In the Edit Relation dialog box, select the Orders table as the child element, and then click OK. This creates a new DataRelation named CustomersOrders in the DataSetschema.
4.On the File menu, click Save to save the changes.
Fill the Typed DataSet
This section adds code to fill the typed DataSet and to map the table names. This section also uses the DataGrid control to display the filled DataSet.
1.Drag a DataGrid control from the toolbox to the form.
2.Double-click the form, and then add the following code to the Load event of the form:
Dim da As New SqlDataAdapter("sp_GetCustOrd", SqlConnection1)
da.SelectCommand.CommandType = CommandType.StoredProcedure
da.TableMappings.Add("Table", "Customers")
da.TableMappings.Add("Table1", "Orders")
Dim ds As New CustOrd() ' Change this name to match .xsd file name.
da.Fill(ds)
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Customers"
NOTE: You must change "CustOrd" to match the name of the .xsd file that you created in the Create the Typed DataSet section.
3.Add the following namespace reference at the top of the code window:
Imports System.Data.SqlClient
4.On the File menu, click Save to save the changes.
5.Press F5 to run the application.
Comments
Post a Comment
Thanking you for the comment.