Solved

Create Class in C# based on Data Source

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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 article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
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…

626 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