Mapping Objects to Relational Databases (MySQL!)


Source Code: MappingObjsLC.zip

This article explains how you can map databasetables to C# objects. In other words, The application generates C#Class files for each table in a relational database. I have used Mysqland ODBC.NET for this project. The application only supports MySQLright now. However, it is easy to port to other databases. I will do itfor the second version of this application. How you can download MySQLand 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 MySQL3.23.43 server, and install it in your Windows if you have Windowsrunning on your computer.

MySQL can be downloaded from www.mysql.com.

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

then you will see

pic1 Mapping Objects to Relational Databases (MySQL!)

Enter, Windows DSN Name as you wish.

MySQL Host is either remote computer or localcomputer, If you have installed MySQL server to your computer, thenchoose localhost.

MySQL DataBase Name: Before entering namehere, you should create database in MySQL server and tables. I canrecommend a software you can download fromhttp://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 usethe 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 ofall, as I said before, It supports Odbc connection, so you have tosetup ODBC. You can name it as you want. Then click on Get Tablesbutton to receive tables from Database and you can see them in thetreeview at the left. After that, Just Enter the Namespace that youwant 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.

pic2 Mapping Objects to Relational Databases (MySQL!)

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

Related Articles :

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!