TableManager.cs :  » SQL-Clients » Database-Commander » YariSoft » DBCommander » 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 » DBCommander » TableManager.cs
using System;
using System.Data;
using System.Data.OleDb;
using System.Windows.Forms;
using YariSoft.DBUtil;
//using Hargal;

namespace YariSoft.DBCommander{
  public class TableManager : IManager
  {
    #region Local variables
    #endregion

    #region Properties
    #endregion

    #region Constructor/Destructor
    public TableManager( OleDbConnection Connecton, SchemaGenerator SchemaGenerator )
      : base ( Connecton, SchemaGenerator )
    {
    }

    public override void Dispose(){
    }
    #endregion

    #region Public functions
    public override DataView FillData( object SelectValue )
    {
      DataView result = null;
      string tableName = SelectValue.ToString().Trim();
      if( tableName == "" ){
        return result;
      }

      result = this.GetData( tableName );
      if( result != null ){
        return result;
      }

      if( !this.OpenConnection() ){
        return result;
      }
      try {
        
        YOleDbAdapter adapter = null;
        if( this.schema.Adapters.ContainsKey( tableName ) ){
          adapter = ( YOleDbAdapter )this.schema.Adapters[ tableName ];
        } else {
          adapter = this.PrepareDataAdapter ( tableName );
          this.schema.Adapters.Add( tableName, adapter );
        }
        
        adapter.DataAdapter.Fill( this.schema.Schema, tableName );
        result = this.schema.Schema.Tables[tableName].DefaultView;
        this.needRefresh = false;
      } catch(Exception Exp ) {
        YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
        if( result != null ){
          result.Dispose();
        }
      }

      this.CloseConnection();
      return result;
    }

    public override DataView GetData( object SelectValue )
    {
      this.schema.Schema.Tables[SelectValue.ToString()].DefaultView.RowFilter = "";
      if( this.schema.Schema.Tables[SelectValue.ToString()].DefaultView.Count > 0 ){
        return this.schema.Schema.Tables[SelectValue.ToString()].DefaultView;
      }
      return null;
    }


    public override DataView RefreshData( object SelectValue )
    {
      DataView result = null;
      string tableName = SelectValue.ToString().Trim();
      if( tableName == "" ){
        return result;
      }

      if( this.schema.Schema.Tables[tableName] != null ){
        while( this.schema.Schema.Tables[tableName].Rows.Count < this.schema.Schema.Tables[tableName].DefaultView.Count ){
          this.schema.Schema.Tables[tableName].DefaultView.Delete( this.schema.Schema.Tables[tableName].DefaultView.Count - 1 );
        }
        this.schema.Schema.Tables[tableName].Clear();
      }

      return this.FillData( SelectValue ) ;
    }


    public override bool UpdateData( object UpdateObject )
    {
      bool result = true;
      if( (( DataTable )UpdateObject).GetChanges() == null ){
        return result;
      }
    
      YOleDbAdapter adapter = null;
      try{
        DataTable changes = (( DataTable )UpdateObject);
        if( changes == null ){
          return true;
        }
        adapter = ( YOleDbAdapter )this.schema.Adapters[(( DataTable )UpdateObject).TableName];
        if( adapter != null ){
          adapter.DataAdapter.Update( changes );
        }
      } catch( Exception Exp ) {
        result = false;
        if ( Exp is DBConcurrencyException && adapter != null ){
          result = YariSoft.DBUtil.Util.TryToSaveConcurrencyData ( ( DBConcurrencyException )Exp, adapter.DataAdapter );
        }
        if( !result ){
          YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
        }
        (( DataTable )UpdateObject).RejectChanges();

        this.needRefresh = true;
      }

      return result;
    }

    public override bool FillTableDataByFK ( string FKName )
    {
      string childTableName = YariSoft.DBUtil.Util.GetTableNameByFK( FKName, this.schema.Schema );

      if( childTableName != "" ){
        if( this.schema.Schema.Tables[childTableName].Rows.Count > 0 ){
          return true;
        }
        this.FillData( childTableName );  
        return true;
      }
      return false;
    }
    #endregion

    #region Private functions
    private YOleDbAdapter PrepareDataAdapter ( string TableName )
    {
      OleDbDataAdapter dataAdapter = new System.Data.OleDb.OleDbDataAdapter( "SELECT * FROM "+ DBUtil.Util.GetTableName( TableName ), this.connecton );
      OleDbCommandBuilder commandBuilder = new OleDbCommandBuilder( dataAdapter );

      commandBuilder.QuotePrefix = "[";
      commandBuilder.QuoteSuffix = "]";
      return new YOleDbAdapter ( dataAdapter, commandBuilder );
    }

    private void OnListChanged(object sender, System.ComponentModel.ListChangedEventArgs args)
    {
      if( args.ListChangedType == System.ComponentModel.ListChangedType.ItemAdded ||
        args.ListChangedType == System.ComponentModel.ListChangedType.ItemChanged ||
        args.ListChangedType == System.ComponentModel.ListChangedType.ItemDeleted ){
        this.UpdateData( (( DataView )sender).Table );
      }
    }

    private void OnDataTableChanged(object sender, System.Data.DataRowChangeEventArgs e) 
    { 
      this.UpdateData( sender );
    }

    #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.