C#
  Home arrow C# arrow Page 2 - LINQ-to-MySQL with DbLinq in C#
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

LINQ-to-MySQL with DbLinq in C#
By: Barzan "Tony" Antal
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2009-03-12

    Table of Contents:
  • LINQ-to-MySQL with DbLinq in C#
  • Preliminary Tasks
  • Let's Do It!
  • Final Thoughts

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    LINQ-to-MySQL with DbLinq in C# - Preliminary Tasks


    (Page 2 of 4 )

    First you need to download the latest release of DbLinq. As we discussed in our previous article, both the source code and the archived (.zip) versions can be downloaded via Google Code. The project is featured as dblinq2007. For now you should download the archived version because it contains the binaries as well.

    Take a minute or two to familiarize yourself with the structure of DbLinq (such as its folders and files). You are going to find DbMetal.exe under the build folder. That is the file that generates the C# class from the database schema and mapping attributes (ORM). It is a command-line utility; currently there is a GUI version of it n development. Under the srcDbMetal folder you are going to find numerous batch files as examples.

    Those aforementioned batch files are called something like run_myMetal.bat; that suggests it gives an example of how to run the DbMetal utility with its arguments in the case of a MySQL server. The same applies for run_oraMetal.bat, run_sqliteMetal.bat, and so forth. Now we are going to copy the run_myMetal.bat into the build folder where the DbMetal executable is to be found, and edit the batch file accordingly.

    DbMetal.exe -provider=MySql -database:your_db -server:your_server -user:root -password:your_pass -namespace:namespace -code:filename.cs -sprocs

    Running the batch file will create the filename.cs C# class that "describes" your MySQL database along with its tables, and has the necessary resources so that you can work on it later and implement the provider. Now you need to copy the following files into the directory of your project and include them into your project:

    • DbLinq.MySql.dll

    • MySql.Data.dll

    • And the newly generated C# class file

    The output of the DbMetal.exe in the case of successful execution is the following:

    DbLinq Database mapping generator 2008 version 0.18.0.0

    for Microsoft (R) .NET Framework version 3.5

    Distributed under the MIT license (http://linq.to/db/license)

     

    >>> Reading schema from MySQL database

    <<< writing C# classes in file 'hotelsdb.cs'

    Should the script (batch file) fail, then there are problems with the arguments you specified in the batch file, such as privileges, invalid password, and so forth. Moving on, when you first include the generated C sharp class file, you may eventually compile the project, and if there are errors, let's fix those. There won't be any algorithm-related problems, but rather issues like ambiguous references, some objects that cannot be resolved, etc.

    For example, in my case, there were ambiguous references between the following:

    System.Data.Linq.Table<hotels_Clients> and DbLinq.Data.Linq.Table<hotels.Clients>

    And the error was found in the following snippet:

    public Table<Clients> Clients { get { return GetTable<Clients>(); } }

    As you can see, the IDE couldn't resolve the references. The type "Table" is to be found in both "System.Data.Linq" and "DbLinq.Data.Linq." The first is the native LINQ while the latter is our DbLinq provider. Therefore, to alleviate this ambiguous reference we can easily rewrite the code as follows (this is one of  many solutions, though):

    public DbLinq.Data.Linq.Table<Clients> Clients { get { return GetTable<Clients>(); } }

    Anyway, with basic knowledge of C# and knowing its syntax, you shouldn't have any problems getting around these errors. In a nutshell, the entire database schema is mapped into the class. Another error that may happen is that, within the using directives at the beginning of the class, the following cannot be resolved: "DbLinq.Linq" and "DbLinq.Linq.Mapping." Write them as DbLinq.Data.Linq and DbLinq.Data.Linq.Mapping. Simply put, the structure differs a bit.

    More C# Articles
    More By Barzan "Tony" Antal


       · Hi, Its very nice article on Linq to sql but i m having some problem when i m...
     

    C# ARTICLES

    - Coding a CRC-Generating Algorithm in C
    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek