/// <summary> /// Writes data contained in DataTable to table stored in TableName property. /// </summary> /// <param name="dtList">List of temp file names containing data tables with grid rows to be output.</param> /// <returns>True if output operation is successful. False if write fails.</returns> public bool WriteDataToOutput(PFList <string> dtList) { bool success = true; PFDatabase db = null; bool createTable = false; DataTable dt = new DataTable(); string tableName = string.Empty; bool isOracleOdbc = false; bool isOracleOledb = false; try { if (dtList.Count == 0) { _msg.Length = 0; _msg.Append("WriteDataToOutput for list of temp files has failed."); _msg.Append(Environment.NewLine); _msg.Append("Temp file name list is empty."); throw new System.Exception(_msg.ToString()); } //Load first temp file in list to get data table schema dt.Rows.Clear(); dt.ReadXml(dtList[0]); db = new PFDatabase(this.DbPlatform, this.DbDllPath, this.DbNamespace + "." + this.DbClassName); db.ConnectionString = this.ConnectionString; tableName = this.TableName; //workaround for oracle odbc and oledb driver problems with inserts if (this.DbPlatform == DatabasePlatform.ODBC) { PFOdbc odbcDb = new PFOdbc(); odbcDb.ConnectionString = this.ConnectionString; DatabasePlatform odbcDbPlat = odbcDb.GetDatabasePlatform(); if (odbcDbPlat == DatabasePlatform.OracleNative || odbcDbPlat == DatabasePlatform.MSOracle) { isOracleOdbc = true; } odbcDb = null; } if (this.DbPlatform == DatabasePlatform.OLEDB) { PFOleDb oledbDb = new PFOleDb(); oledbDb.ConnectionString = this.ConnectionString; DatabasePlatform oledbDbPlat = oledbDb.GetDatabasePlatform(); if (oledbDbPlat == DatabasePlatform.OracleNative || oledbDbPlat == DatabasePlatform.MSOracle) { isOracleOledb = true; } oledbDb = null; } //set table and column names to upper case if this is an Oracle ODBC driver or OLEDB provider if (isOracleOdbc || isOracleOledb) { tableName = this.TableName.ToUpper(); for (int colInx = 0; colInx < dt.Columns.Count; colInx++) { dt.Columns[colInx].ColumnName = dt.Columns[colInx].ColumnName.ToUpper(); } } else { tableName = this.TableName; } //end workaround for oracle odbc and oledb dt.TableName = tableName; if (db.TableExists(tableName)) { if (this.ReplaceExistingTable) { db.OpenConnection(); db.DropTable(tableName); db.CloseConnection(); createTable = true; } else { createTable = false; //existing table will be imported into } } else { createTable = true; } if (createTable) { db.OpenConnection(); string createScript = string.Empty; string errorMessages = string.Empty; bool tabCreated = db.CreateTable(dt, out createScript, out errorMessages); if (tabCreated == false) { _msg.Length = 0; _msg.Append("Unable to create table: "); _msg.Append(tableName); _msg.Append(Environment.NewLine); _msg.Append("Error Messages: "); _msg.Append(errorMessages); _msg.Append(Environment.NewLine); _msg.Append("Create Script: "); _msg.Append(createScript); throw new System.Exception(_msg.ToString()); } db.CloseConnection(); } for (int dtInx = 0; dtInx < dtList.Count; dtInx++) { _msg.Length = 0; _msg.Append("List # "); _msg.Append(dtInx.ToString()); Console.WriteLine(_msg.ToString()); dt = new DataTable(); dt.TableName = this.TableName; //original table name should be in xml file definition dt.Rows.Clear(); dt.ReadXml(dtList[dtInx]); db.OpenConnection(); //set table and column names to upper case if this is an Oracle ODBC driver or OLEDB provider if (isOracleOdbc || isOracleOledb) { dt.TableName = this.TableName.ToUpper(); for (int colInx = 0; colInx < dt.Columns.Count; colInx++) { dt.Columns[colInx].ColumnName = dt.Columns[colInx].ColumnName.ToUpper(); } } db.ImportDataFromDataTable(dt, this.OutputBatchSize); db.CloseConnection(); dt = null; } } catch (System.Exception ex) { success = false; _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); throw new System.Exception(_msg.ToString()); } finally { if (db != null) { if (db.IsConnected) { db.CloseConnection(); } db = null; } } return(success); }
/// <summary> /// Writes data contained in DataTable to table stored in TableName property. /// </summary> /// <param name="dt">DataTable object containing data to be output.</param> /// <returns>True if output operation is successful. False if write fails.</returns> public bool WriteDataToOutput(DataTable dt) { bool success = true; PFDatabase db = null; bool createTable = false; DataTable saveDtSchema = null; string tableName = string.Empty; bool isOracleOdbc = false; bool isOracleOledb = false; try { //save column names that may be changed during export processing so that they can be restored in finally block below saveDtSchema = dt.Clone(); db = new PFDatabase(this.DbPlatform, this.DbDllPath, this.DbNamespace + "." + this.DbClassName); db.ConnectionString = this.ConnectionString; tableName = this.TableName; //workaround for oracle odbc and oledb driver problems with inserts if (this.DbPlatform == DatabasePlatform.ODBC) { PFOdbc odbcDb = new PFOdbc(); odbcDb.ConnectionString = this.ConnectionString; DatabasePlatform odbcDbPlat = odbcDb.GetDatabasePlatform(); if (odbcDbPlat == DatabasePlatform.OracleNative || odbcDbPlat == DatabasePlatform.MSOracle) { isOracleOdbc = true; } odbcDb = null; } if (this.DbPlatform == DatabasePlatform.OLEDB) { PFOleDb oledbDb = new PFOleDb(); oledbDb.ConnectionString = this.ConnectionString; DatabasePlatform oledbDbPlat = oledbDb.GetDatabasePlatform(); if (oledbDbPlat == DatabasePlatform.OracleNative || oledbDbPlat == DatabasePlatform.MSOracle) { isOracleOledb = true; } oledbDb = null; } //set table and column names to upper case if this is an Oracle ODBC driver or OLEDB provider if (isOracleOdbc || isOracleOledb) { tableName = this.TableName.ToUpper(); for (int colInx = 0; colInx < dt.Columns.Count; colInx++) { dt.Columns[colInx].ColumnName = dt.Columns[colInx].ColumnName.ToUpper(); } } //end workaround for oracle odbc and oledb dt.TableName = tableName; if (db.TableExists(tableName)) { if (this.ReplaceExistingTable) { db.OpenConnection(); db.DropTable(tableName); db.CloseConnection(); createTable = true; } else { createTable = false; //existing table will be imported into } } else { createTable = true; } if (createTable) { db.OpenConnection(); string createScript = string.Empty; string errorMessages = string.Empty; bool tabCreated = db.CreateTable(dt, out createScript, out errorMessages); if (tabCreated == false) { _msg.Length = 0; _msg.Append("Unable to create table: "); _msg.Append(tableName); _msg.Append(Environment.NewLine); _msg.Append("Error Messages: "); _msg.Append(errorMessages); _msg.Append(Environment.NewLine); _msg.Append("Create Script: "); _msg.Append(createScript); throw new System.Exception(_msg.ToString()); } db.CloseConnection(); } db.OpenConnection(); db.ImportDataFromDataTable(dt, this.OutputBatchSize); db.CloseConnection(); } catch (System.Exception ex) { success = false; _msg.Length = 0; _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex)); throw new System.Exception(_msg.ToString()); } finally { if (db != null) { if (db.IsConnected) { db.CloseConnection(); } db = null; } //resstore any column names that may have been changed during export processing for (int colInx = 0; colInx < dt.Columns.Count; colInx++) { dt.Columns[colInx].ColumnName = saveDtSchema.Columns[colInx].ColumnName; } } return(success); }