Solved

Create Class in C# based on Data Source

Posted on 2006-07-10
8
568 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now