Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Create Class in C# based on Data Source

Posted on 2006-07-10
8
Medium Priority
?
581 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Accepted Solution

by:
shy_talk earned 172 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 164 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 164 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

704 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