Solved

Create Class in C# based on Data Source

Posted on 2006-07-10
8
569 Views
Last Modified: 2012-05-05
I have built a few applications using .NET, but am still learning my way around.  I have a SQL database that I want to use in my applicaiton.  I have connected and have created datasources pointing to my tables, called HEADER and DETAIL.  I want to create classes called HEADER and DETAIL to refer to them in my code, but don't know how to do this.  Maybe I am trying hard to do something that is unnecessary, but previously, I manually typed in all the fields to create the class, but I know there is a better way.  I can open the dataset designer and see the SQLDataSet.xsd which contains definitions for DETAIL and HEADER.  

How can I use this so that in my code I can do something like

DETAIL myDetail = new DETAIL();
HEADER myHeader = new HEADER();
...
...
Field1 = HEADER.name;
Field2 = DETAIL.number;

etc.  

Isn't there an easy way to say build a class based on the data source???
0
Comment
Question by:MGothelf
8 Comments
 
LVL 2

Accepted Solution

by:
shy_talk earned 43 total points
ID: 17073308
You probably need to create a Typed DataSet, which automatically creates classes that model your data structure. Creating a Typed DataSet creates a class inside your dataset.  This allows you to refer to all your fields as properties of a class, e.g. MyDetail.Name rather than by textual names, e.g. MyDetail("Name")

There are other things you can do, such as outputting the classes to XML, but the help files should reveal all (sample below).

Hope this is of some use.

Typed Versus Untyped Datasets
A typed dataset is a dataset that is first derived from the base DataSet class and then uses information from the Dataset Designer, which is stored in an .xsd file, to generate a new strongly-typed dataset class. Information from the schema (tables, columns, and so on) is generated and compiled into this new dataset class as a set of first-class objects and properties. Because a typed dataset inherits from the base DataSet class, the typed class assumes all of the functionality of the DataSet class and can be used with methods that take an instance of a DataSet class as a parameter

An untyped dataset, in contrast, has no corresponding built-in schema. As in a typed dataset, an untyped dataset contains tables, columns, and so on — but those are exposed only as collections. (However, after manually creating the tables and other data elements in an untyped dataset, you can export the dataset's structure as a schema using the dataset's WriteXmlSchema method.)

Contrasting Data Access in Typed and Untyped Datasets
The class for a typed dataset has an object model in which its properties take on the actual names of the tables and columns. For example, if you are working with a typed dataset, you can reference a column using code such as the following:

Visual Basic  Copy Code
' This accesses the CustomerID column in the first row of the Customers table.
Dim customerIDValue As String = NorthwindDataSet.Customers(0).CustomerID

 
C#  Copy Code
// This accesses the CustomerID column in the first row of the Customers table.
string customerIDValue = northwindDataSet.Customers[0].CustomerID;

 
J#  Copy Code
// This accesses the CustomerID column in the first row of the Customers table.
String customerIDValue =
    northwindDataSet.get_Customers().get_Item(0).get_CustomerID();

 

In contrast, if you are working with an untyped dataset, the equivalent code is:

Visual Basic  Copy Code
Dim customerIDValue As String = _
    CType(dataset1.Tables("Customers").Rows(0).Item("CustomerID"), String)

 
C#  Copy Code
string customerIDValue = (string)
    dataset1.Tables["Customers"].Rows[0]["CustomerID"];

 
0
 

Author Comment

by:MGothelf
ID: 17073498
Yes, thank you.  I know why a typed data set is better.  I don't know how to create one.

I guess my question should have been "How do I create a typed dataset?".
0
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 41 total points
ID: 17073549
This should help:

HOW TO: Create and Use a Typed DataSet by Using Visual C# .NET
http://support.microsoft.com/default.aspx?scid=kb;en-us;320714
0
 

Author Comment

by:MGothelf
ID: 17073960
to mrichmon:

That helped a little.  I was able to create the xml schema before, but doing it the way the article suggested, I now have HEADER.xsd in my solution explorer, but I still don't have a class.

In the article it says the following:

12.      A Schema based on a SQL Server object now exists. To create and fill a typed DataSet, first you must generate the class that maps to the Schema. Right-click the Design view, and then make sure that the Generate DataSet option is selected. If it is not selected, select it. Press CTRL+S to save the Schema and generate the class.


However, when I right click the design view (I assume it means the design view of Header.xsd), there is not option to Generate DataSet.

0
 
LVL 7

Assisted Solution

by:whatsit2002
whatsit2002 earned 41 total points
ID: 17078474
I have a project on GotDotNet that may or may not be helpful for you. You can find it at:
http://www.gotdotnet.com/codegallery/codegallery.aspx?id=8c4900ec-02fc-4b86-a17e-200fd719e2ea

It does NOT create typed data sets. It creates custom object classes based on your database objects. It also creates a skeleton of a data access layer for you that does simple select, update/insert, and delete.

If you want, you can give it a try and see if it does what you're looking for.

Jason
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question