IdentityDataOperation.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 » IdentityDataOperation.cs
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections; 
using System.Windows.Forms; 

namespace YariSoft.DBUtil{
  public class IdentityDataOperation :DBBaseOperation
  {
    #region Local variables
    private ArrayList    identityColumns    = null;
    private ArrayList    readOnlyColumns    = null;
    private OleDbCommand  identityCommand    = null;  
    #endregion

    public IdentityDataOperation ( OleDbConnection Connection, DataView Source, DataView Destination, System.Collections.ArrayList SelectedRows ):
      base ( Connection, Source, SelectedRows ) 
    {
      this.destination = Destination;
    }

    public bool Exec( OnDataChangedEventHandler OnDestinationDataChanged )
    {
      bool status = false;
      ConnectionState previousConnectionState = this.connection.State;
      
      try{
        if( previousConnectionState == ConnectionState.Closed ){
          this.connection.Open();
        }

        DataCopyOperation rowOperation = new DataCopyOperation( this.connection, (( DataView )this.source ), (( DataView )this.destination ), this.selectedRows );
        rowOperation.OnDestinationDataChanged += new YariSoft.DBUtil.DBBaseOperation.OnDataChangedEventHandler ( OnDestinationDataChanged );

        this.PrepareIdentityArrays ( this.connection, rowOperation, ( DataView )this.destination );

        if( rowOperation != null ){
          status = rowOperation.Exec();
          rowOperation.Dispose();
        }

      } finally {
        this.DisposeIdentityArrays ( this.connection, (( DataView ) this.destination ).Table.TableName );
      }

      if( previousConnectionState == ConnectionState.Closed ){
        this.connection.Close();
      }

      return status;
    }

    #region Private functions
    private void PrepareIdentityArrays( OleDbConnection DestConnection, DataCopyOperation RowOperation, DataView Destination )
    {
      if( this.identityColumns == null ){
        this.identityColumns = new ArrayList();
      }
      if( this.readOnlyColumns  == null ){
        this.readOnlyColumns = new ArrayList();
      }
      
      foreach( DataColumn column in Destination.Table.Columns ){
        if( column.AutoIncrement ){
          identityColumns.Add( column );
          column.AutoIncrement = false;
          if( column.ReadOnly ){
            column.ReadOnly = false;
            readOnlyColumns.Add( column );
          }
        }
      }
      if( this.ResetIdentityState( DestConnection, Destination.Table.TableName, true ) ){
        this.PrepareInsertCommand ( Destination.Table, DestConnection );
        if( identityColumns.Count > 0 ){
          RowOperation.OnNeedInsertIdentity += new DataCopyOperation.OnNeedInsertIdentityEventHandler( this.OnNeedInsertIdentity );
        }
      }
    }

    private void PrepareInsertCommand( DataTable Table, OleDbConnection DestConnection )
    {
      string insertSQL = "INSERT INTO [" + Table.TableName + "] (";
      string paramNames = "";
      
      foreach( DataColumn column in Table.Columns ){
        if( ! column.ReadOnly ){
          insertSQL += column.ColumnName + ",";
          paramNames += "?,";
        }
      }
      insertSQL  = insertSQL.Substring ( 0, insertSQL.Length - 1 );
      insertSQL += ") VALUES (";
      insertSQL += paramNames.Substring( 0, paramNames.Length - 1 ) + ")";

      this.identityCommand =new OleDbCommand( insertSQL, DestConnection );
    }

    private bool ResetIdentityState( OleDbConnection DestConnection, string TableName, bool Reset ) 
    {
      bool status = true;
      string SQL = "SET IDENTITY_INSERT [" + TableName + "] ";
      if( Reset ){
        SQL += "ON";
      } else {
        SQL += "OFF";
      }

      if( SQL != "" ){
        ConnectionState prevState = DestConnection.State;
        try{
          if( prevState == ConnectionState.Closed ){
            DestConnection.Open();
          }
          OleDbCommand command = new OleDbCommand ( SQL, DestConnection );
          command.ExecuteNonQuery();
          command.Dispose();
        }catch( Exception Exp ){
          YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
          status = false;
        }
        if( prevState == ConnectionState.Closed ){
          DestConnection.Close();
        }
      }

      return status;
    }

    private void DisposeIdentityArrays( OleDbConnection DestConnection, string TableName )
    {
      this.ResetIdentityState( DestConnection, TableName, false );

      foreach( DataColumn column in this.identityColumns ){
        column.AutoIncrement = true;
      }

      foreach( DataColumn column in this.readOnlyColumns ){
        column.ReadOnly = true;
      }
      this.identityColumns.Clear();
      this.readOnlyColumns.Clear();
      if( this.identityCommand != null ){
        this.identityCommand.Dispose();
      }
    }

    private bool OnNeedInsertIdentity( DataTable TableToUpdate, DataRow RowToUpdate, ref bool Updated )
    {
      Updated = false;
      try{
        this.identityCommand.Parameters.Clear();
        foreach( DataColumn column in TableToUpdate.Columns ){
          if( ! column.ReadOnly ){
            this.identityCommand.Parameters.Add( new OleDbParameter( column.ColumnName, RowToUpdate[ column ] ) );
          }
        }
        if( this.identityCommand.ExecuteNonQuery() == 1 ){
          Updated = true;
        }
      } catch ( Exception Exp ){
        YariSoft.Utils.YMessages.Show( Exp.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error );
        return false;
      }
      
      return true;
    }
    #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.