Пример #1
0
        public static void ImportDataTableTest(MainForm frm)
        {
            PFOleDb   db = new PFOleDb();
            string    connectionString  = string.Empty;
            string    originalTableName = string.Empty;
            DataTable dt = null;

            try
            {
                if (frm.cboConnectionString.Text.ToLower().Contains("ifxoledbc"))
                {
                    _msg.Length = 0;
                    _msg.Append("Import table does not work with Informix driver. Driver does not have capability of retrieving schema information.");
                    throw new System.Exception(_msg.ToString());
                }

                _msg.Length = 0;
                _msg.Append("ImportDataTableTest started ...");
                Program._messageLog.WriteLine(_msg.ToString());

                connectionString = frm.cboConnectionString.Text;

                db.ConnectionString = connectionString;


                db.OpenConnection();

                StringBuilder sql = new StringBuilder();
                dt = frm.keyValsDataSet.Tables["KeyValTable"];
                originalTableName = dt.TableName;
                string tableName = frm.txtTableName.Text;


                //first delete table if it already exists
                Program._messageLog.WriteLine("\r\nDropping old table if it exists ...");


                string catalogName = string.Empty;
                string schemaName  = string.Empty;
                string tabName     = string.Empty;

                catalogName = frm.txtCatalogName.Text;
                schemaName  = frm.txtSchemaName.Text;
                tabName     = "KeyValTable";
                if (schemaName.Trim().Length > 0)
                {
                    dt.TableName = schemaName + "." + tabName;
                }

                if (db.TableExists(catalogName, schemaName, tabName))
                {
                    bool dropped = db.DropTable(catalogName, schemaName, tabName);
                    if (dropped == false)
                    {
                        _msg.Length = 0;
                        _msg.Append("Unable to drop table ");
                        if (catalogName.Trim().Length > 0)
                        {
                            _msg.Append(catalogName);
                            _msg.Append(".");
                        }
                        if (schemaName.Trim().Length > 0)
                        {
                            _msg.Append(schemaName);
                            _msg.Append(".");
                        }
                        _msg.Append(tabName);
                        throw new DataException(_msg.ToString());
                    }
                    else
                    {
                        _msg.Length = 0;
                        _msg.Append("Old table dropped: ");
                        if (catalogName.Trim().Length > 0)
                        {
                            _msg.Append(catalogName);
                            _msg.Append(".");
                        }
                        if (schemaName.Trim().Length > 0)
                        {
                            _msg.Append(schemaName);
                            _msg.Append(".");
                        }
                        _msg.Append(tabName);
                        Program._messageLog.WriteLine(_msg.ToString());
                    }
                }


                Program._messageLog.WriteLine("\r\nCreating a table in the database ...");

                //create the table
                bool tableCreated = db.CreateTable(dt);

                if (tableCreated)
                {
                    db.ImportDataFromDataTable(dt);
                }
                else
                {
                    _msg.Length = 0;
                    _msg.Append("CreateTable for ");
                    _msg.Append(dt.TableName);
                    _msg.Append(" failed.");
                    Program._messageLog.WriteLine(_msg.ToString());
                }

                db.CloseConnection();
            }
            catch (System.Exception ex)
            {
                _msg.Length = 0;
                _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                Program._messageLog.WriteLine(_msg.ToString());
                AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
            }
            finally
            {
                if (dt != null)
                {
                    if (originalTableName.Length > 0)
                    {
                        dt.TableName = originalTableName;
                    }
                }
                _msg.Length = 0;
                _msg.Append("... ImportDataTableTest finished.");
                Program._messageLog.WriteLine(_msg.ToString());
            }
        }