DBSchema.cs :  » SQL-Clients » Database-Commander » YariSoft » DBUtil » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » SQL Clients » Database Commander 
Database Commander » YariSoft » DBUtil » DBSchema.cs
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using System.Collections;

namespace YariSoft.DBUtil{
  public class SchemaGenerator
  {
    #region Local variables
    private DataSet _schema = null; 
    private DataTable _tables = null;
    private Hashtable _adapters = null;
    #endregion

    #region Properties
    public DataSet Schema 
    {
      get{ return this._schema; }
    }

    public DataTable Tables 
    {
      get{ return this._tables ; }
    }

    public Hashtable Adapters 
    {
      get{ return this._adapters ; }
    }


    #endregion

    #region Constructor/Destructor
    public SchemaGenerator()
    {
      this._schema = new DataSet();
      this._adapters = new Hashtable();
    }

    ~SchemaGenerator()
    {
      this.Dispose();
    }

    public void Dispose()
    {
      if( this._schema != null ){
        this._schema.Dispose();
      }
      if( this._tables != null ){
        this._tables.Dispose();
      }
      if( this._adapters != null ){
        foreach ( YOleDbAdapter adapter in this._adapters.Values ){
          adapter.Dispose();
        }
      }
    }
    #endregion

    #region Public functions
    public bool BuildSchema( OleDbConnection Connection )
    {
      bool status = true;

      ConnectionState previousConnectionState = Connection.State;
      try{
        if( previousConnectionState == ConnectionState.Closed ){
          Connection.Open();
        }
        this._schema.Relations.Clear();
        foreach( DataTable table in this._schema.Tables ){
          for( int i = table.Constraints.Count - 1; i >= 0; i-- ){
            if( table.Constraints[i] is ForeignKeyConstraint ){
              table.Constraints.Remove(table.Constraints[i]);
            }
          }
        }
        this._schema.Tables.Clear();

        this._tables = Connection.GetOleDbSchemaTable( OleDbSchemaGuid.Tables,
                                    new object[] {null, null, null, "TABLE"});
        this._tables.TableName = "__YS_Tables_Internal";
        foreach ( DataRow Row in this._tables.Rows ){
          if( ! this.AddTable( Connection, ( string )Row["TABLE_NAME"]) ){
            status = false;
            break;
          }
        }
        this._schema.EnforceConstraints = false;
        if( status ){
          status = this.AddConstraints( Connection );
        }

      } catch( Exception Exp ) {
        YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
        status = false;
      }

      if( previousConnectionState == ConnectionState.Closed ){
        Connection.Close();
      }
      return status;
    }
    #endregion

    #region Private functions
    private bool AddTable( OleDbConnection Connection, string TableName )
    {
      try {
        System.Data.OleDb.OleDbDataAdapter dataAdapter = new System.Data.OleDb.OleDbDataAdapter( "SELECT * FROM ["+ TableName +"] Where 2 < 1", Connection );
        DataTable[] tables = dataAdapter.FillSchema( this._schema, System.Data.SchemaType.Source );
        foreach ( DataTable table in tables ){
          table.TableName = TableName;
        }
        dataAdapter.Dispose();
      } catch(Exception Exp ) {
        YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
        return false;
      }

      return true;
    }

    private bool AddConstraints( OleDbConnection Connection )
    {
      bool status = true;
      DataTable constraints = null;
      try {
        constraints = Connection.GetOleDbSchemaTable( OleDbSchemaGuid.Foreign_Keys, new object[] {});
        foreach ( DataRow row in constraints.Rows ){
          DataColumn pCol = this._schema.Tables[row["PK_TABLE_NAME"].ToString()].Columns[row["PK_COLUMN_NAME"].ToString()];
          DataColumn fCol = this._schema.Tables[row["FK_TABLE_NAME"].ToString()].Columns[row["FK_COLUMN_NAME"].ToString()];
          this._schema.Relations.Add( row["FK_NAME"].ToString(), pCol, fCol );
        }
      } catch(Exception Exp ) {
        YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
        status = false;
      }

      if( constraints != null ){
        constraints.Dispose();
      }
      return status;
    }
    #endregion
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.