Previous Next
Programmers Guide for .NET Chapter 2 - Configuring Source and Library in Visual Studio .NET 2003

Chapter 2 - Configuring Source and Library in Visual Studio .NET 2003

Microsoft Visual Studio .NET 2003 is an advanced integrated development environment by Microsoft. It lets programmers to create programs that run on Microsoft Windows and the World Wide Web.

In chapter 2, you have generated C# code, exported database schema (DDL) and created persistent library. Now you can create a project for the generated C# code in Visual Studio .NET 2003.

In this chapter:

Copying Generated Source and Library to Visual Studio .NET Project

You can create Visual Studio .NET project easily because DB-VA supports the generation of C# project file. The project file helps you to configure the generated C# classes, resources files and referenced libraries in Visual Studio .NET.

  1. Select C# Project File in the Database Code Generation dialog box
  2. Figure 2.1 - The C# Proejct File options
  3. The C# Project File is created in the Output Path\src folder
  4. Figure 2.2 - The generated C# Project file
  5. Open Visual Studio .NET 2003. From the menu bar, select Open > Project.
  6. Figure 2.3 - Open a project files
  7. Select the Airport.csproj and click Open
  8. Figure 2.4 - Select a project
  9. All Libraries are added to References and C# classes are inside the project.
  10. Figure 2.5 - The project contains all sources and library

Adding Reference of an Existing Project to DB-VA Generated C# Project

You can also reference the generated C# Visual Studio .NET Project as a library to develop an application.

  1. Open Visual Studio .NET Project. Select File > New > Project on menu bar.
  2. Figure 2.6 - To create a new project
  3. Select Project Types as Visual C# Projects and Templates as Console Application. Enter the Project and Location for the new project and click OK.
  4. Figure 2.7 - New Project dialog
  5. The AirportApplication project is created.
  6. Figure 2.8 - Project created

    You have an existing solution called AirportApplication and you may want to use the generated persistent code to develop a database application in AirportApplication.

  7. Right click Solution > Add > Existing Project... to select the generated Project
  8. Figure 2.9 - To add and existing proejct
  9. Select the generated C# Project File (Airport.csproj) and add it to the existing Solution.
  10. Figure 2.10 - select the project file
  11. The generated C# project is added to Solution.
  12. Figure 2.11 - Proejct imported
  13. Right click Airport and select Properties on menu.
  14. Figure 2.12 - To open the project properties
  15. Change the Output Type from "Windows Application" to "Class Library" . Click OK.
  16. Figure 2.13 - Project Property Page
  17. From the menu bar, select Build > Rebuild Solution. The DLL file is generated.
  18. Figure 2.14 - To rebuild the solution
  19. Right click the AirportApplication project and select Add Reference...on menu.
  20. Figure 2.15 - Add project reference
  21. Select Airport Project in Projects tab.
  22. Figure 2.16 - Add Reference dialog
  23. Select .NET tab and add all the libraries (HashCodeProvider.dll, log4net.dll, MySQL.Data.dll, NHibernate.dll) in Airport project's lib folder.
  24. Figure 2.17 - Select the Component
  25. The Airport project and libraries are added to the AirportApplication project's Reference. You can develop application to call C# persistent classes in the AirportApplication project.
  26. Figure 2.18 - The reference added
  27. Copy the hibernate.cfg.xml file from Airport project\src to AirportApplication project\bin\Debug. The hibernate.cfg.xml contains the information of database connections and other settings.
  28. Figure 2.19 - Copy the hibernate.cfg.xml to debug folder

Testing the Visual Studio .NET Project

You have created the AirportApplication project and referenced the generated Airport project. You can develop a simple program to test the project.

  1. Open the Class1.cs file in AirportApplication.
  2. Modify the Class1.cs file.
  3. using System;
    using airport;
    using Orm;

    namespace AirportApplication
    {
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main(string[] args)
    {
    PersistentTransaction t = airport.AirportPersistentManager.Instance().GetSession().BeginTransaction();
    try
    {
    airport.Flight lairportFlight = airport.FlightFactory.CreateFlight();
    // Initialize the properties of the persistent object
    lairportFlight.ArrivingAirport = "Hong Kong International Airport";
    lairportFlight.DepartingAirport = "Kansai International Airport";
    lairportFlight.DepartureTime = DateTime.Now;

    airport.Plane lairportPlane = airport.PlaneFactory.CreatePlane();
    // Initialize the properties of the persistent object
    lairportPlane.PlaneType = "747 plane";
    lairportPlane.MaxSpeed = 967;
    lairportPlane.MaxDistance = 8232;
    lairportPlane.flights.Add(lairportFlight);
    lairportPlane.Save();
    // lairportPlane.Save();
    t.Commit();
    }
    catch (Exception e)
    {
    t.RollBack();
    Console.WriteLine(e);
    }
    }
    }
    }
  4. From the menu bar, select Build > Rebuild Solution.
  5. Figure 2.20 - To rebuild solution
  6. Select Debug > Start Without Debugging to execute Class1.cs.
  7. Figure 2.21 - To start without debugging
  8. Check the MySQL database. The record is created.
  9. Figure 2.22 - The record is created in the database

Previous Next
Visual Paradigm International Limited
Website: www.visual-paradigm.com
E-mail: support@visual-paradigm.com