Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Team Foundation Server Hosting
Search :       Advanced Search »
Home » ADO.NET & Database » Mapping Objects to Relational Databases

Mapping Objects to Relational Databases

The application generates C# Class files for each table in a relational database. I have used Mysql and ODBC.NET for this project. The application only supports MySQL right now.

Page Views : 6625
Downloads : 119
Rating :
 Rate it
Level : Intermediate
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
MappingObjsLC.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
Team Foundation Server Hosting
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 

Description

This article explains how you can map database tables to C# objects. In other words, The application generates C# Class files for each table in a relational database. I have used Mysql and ODBC.NET for this project. The application only supports MySQL right now. However, it is easy to port to other databases. I will do it for the second version of this application. How you can download MySQL and MySQL ODBC driver. if you browse the web page http://www.mysql.com, you will see that MyODBC 2.50.39 link on the right side of the screen. After that you have download ODBC driver, you should download MySQL 3.23.43 server, and install it in your Windows if you have Windows running on your computer.

MySQL can be downloaded from www.mysql.com.

After downloading and installation, you should configure ODBC driver. Here is it how you can do it. goto Control Panel, run DataSources(ODBC). While you are at USER DSN, click on Add button, Then choose MySQL from the list click on Finish button

then you will see

Enter, Windows DSN Name as you wish.

MySQL Host is either remote computer or local computer, If you have installed MySQL server to your computer, then choose localhost.

MySQL DataBase Name: Before entering name here, you should create database in MySQL server and tables. I can recommend a software you can download from http://dbtools.vila.bol.com.br/

with this tool, you can create databases, tables and so on. When you try to connect to MySQL server, you can use the user name as root and password as just empty.

User: root
Password:

then click on OK button, You are all set. if you have problems with the settings, just let me know.

This application supports

Mapping attributes to columns: a class attribute will map to one column in a relational database.
Mapping classes to tables: Classes map to tables.

So, how you can use this application. First of all, as I said before, It supports Odbc connection, so you have to setup ODBC. You can name it as you want. Then click on Get Tables button to receive tables from Database and you can see them in the treeview at the left. After that, Just Enter the Namespace that you want to put in the C# class files.

the Last thing is that you should enter the folder name that will have these C# class files.

When you click on Generate C# Class Files, it will create all C# files in a given folder.

You can add any datatype you want. therfore, it is going to be easy to port the application to other databases.

private String FindDataType(String Type)
{
if (Type.IndexOf("varchar")!=-1)
{
return "String";
}
else if(Type.IndexOf("datetime")!=-1)
{
return "DateTime";
}
else if(Type.IndexOf("date")!=-1)
{
return "DateTime";
}
else if(Type.IndexOf("decimal")!=-1)
{
return "Decimal";
}
else if(Type.IndexOf("tinyint(4)")!=-1)
{
return "boolean";
}
else if(Type.IndexOf("int")!=-1)
{
return "int";
}
else if(Type.IndexOf("mediumint")!=-1)
{
return "int";
}
else if(Type.IndexOf("timestamp")!=-1)
{
return "DateTime";
}
else if(Type.IndexOf("bigint")!=-1)
{
return "Int64";
}
else if(Type.IndexOf("float")!=-1)
{
return "float";
}
else if(Type.IndexOf("tinyblob")!=-1)
{
return "String";
}
else if(Type.IndexOf("tinytext")!=-1)
{
return "String";
}
else if(Type.IndexOf("text")!=-1)
{
return "String";
}
else if(Type.IndexOf("mediumblob")!=-1)
{
return "String";
}
else if(Type.IndexOf("mediumtext")!=-1)
{
return "String";
}
else if(Type.IndexOf("longblob")!=-1)
{
return "String";
}
else if(Type.IndexOf("longtext")!=-1)
{
return "String";
}
// Source Code End

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Levent Camlibel
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Discover the top 5 tips for understanding .NET Interop
Become a Sponsor
 Comments
Map your object Graphs visually to database queries results and query parameters visually. by Neophytos On April 5, 2009
The Orasis Mapping Studio 2009 Database Edition Library Reflector allows developers to utilize their class libraries that do not and should not resemble the physical database table schemas. With no requirements on the development of the data models such as attributing them or implementing interfaces or extending third party classes or even requiring them to contain ID properties, developers can simply provide a default constructor and start mapping! The Reflector brings in all of the needed assembly dependencies in the project. Developers can even add library dependencies the same way that they are familiar with. The Studio’s Field Mapping control enables nested object graph mappings between SQL columns/parameters and object fields. Use the pure object models that you have generated by visually mapping them to query results and parameters. Visually expand nested class references or simply replace them with derived instances. Replace interfaces with their implementation classes, or simply replace an abstract class with one of its implementation classes. The Studio’s Smart Data Access code generation engine will minimize the number of statements that are required to select, insert, update and delete whether the mappings target queries or stored procedures. With full knowledge of database metadata at design time, .Net object metadata and standard .Net conversions the code generation engine produces extremely efficient code free of any reflection code. Start Mapping object graphs to your database queries at http://www.orasissoftware.com
Reply | Email | Modify 
Team Foundation Server Hosting
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.