Example #1
0
        public static void DataReaderToDataTableTest()
        {
            PFSQLAnywhere db = new PFSQLAnywhere();
            string        connectionString = string.Empty;
            Stopwatch     sw = new Stopwatch();

            try
            {
                db.DatabasePath      = _frm.txtDatabaseFile.Text;
                db.DataSourceName    = _frm.txtDataSource.Text;
                db.ServerName        = _frm.txtServerName.Text;
                db.DatabaseName      = _frm.txtDatabaseName.Text;
                db.Username          = _frm.txtUsername.Text;
                db.Password          = _frm.txtPassword.Text;
                db.DatabaseKey       = _frm.txtDatabaseKey.Text;
                db.EncryptedPassword = _frm.txtEncryptedPassword.Text;
                db.Encryption        = _frm.txtEncryption.Text;

                connectionString = db.ConnectionString;

                _msg.Length = 0;
                _msg.Append("Connection string is: \r\n");
                _msg.Append(connectionString);
                Program._messageLog.WriteLine(_msg.ToString());

                if (_frm.txtSQLQuery.Text.Length == 0)
                {
                    throw new System.Exception("You must specify a SQL query to run.");
                }

                sw.Start();

                db.OpenConnection();

                db.SQLQuery = _frm.txtSQLQuery.Text;
                if (_frm.chkIsStoredProcedure.Checked)
                {
                    db.CommandType = CommandType.StoredProcedure;
                }
                else
                {
                    db.CommandType = CommandType.Text;
                }

                sw.Stop();
                _msg.Length = 0;
                _msg.Append("Open connection time: ");
                _msg.Append(sw.FormattedElapsedTime);
                Program._messageLog.WriteLine(_msg.ToString());

                sw.Start();

                SADataReader rdr = (SADataReader)db.RunQueryDataReader();
                DataTable    tab = db.ConvertDataReaderToDataTable(rdr);
                Program._messageLog.WriteLine("Table columns count: " + tab.Columns.Count.ToString());
                rdr.Close();

                for (int i = 0; i < tab.Rows.Count; i++)
                {
                    DataRow r = tab.Rows[i];
                    _msg.Length = 0;
                    int maxColInx = tab.Columns.Count - 1;
                    for (int ci = 0; ci <= maxColInx; ci++)
                    {
                        _msg.Append(tab.Columns[ci].ColumnName);
                        _msg.Append(": ");
                        _msg.Append(r[ci].ToString());
                        if (ci < maxColInx)
                        {
                            _msg.Append(", ");
                        }
                    }
                    Program._messageLog.WriteLine(_msg.ToString());
                }

                sw.Stop();
                _msg.Length = 0;
                _msg.Append("Table read time: ");
                _msg.Append(sw.FormattedElapsedTime);
                Program._messageLog.WriteLine(_msg.ToString());
            }
            catch (System.Exception ex)
            {
                _frm.OutputErrorMessageToLog(ex);
            }
            finally
            {
                db.CloseConnection();
                db = null;
            }
        }
Example #2
0
        public static void DataReaderTest()
        {
            PFSQLAnywhere db = new PFSQLAnywhere();
            string        connectionString = string.Empty;
            Stopwatch     sw = new Stopwatch();

            try
            {
                db.DatabasePath      = _frm.txtDatabaseFile.Text;
                db.DataSourceName    = _frm.txtDataSource.Text;
                db.ServerName        = _frm.txtServerName.Text;
                db.DatabaseName      = _frm.txtDatabaseName.Text;
                db.Username          = _frm.txtUsername.Text;
                db.Password          = _frm.txtPassword.Text;
                db.DatabaseKey       = _frm.txtDatabaseKey.Text;
                db.EncryptedPassword = _frm.txtEncryptedPassword.Text;
                db.Encryption        = _frm.txtEncryption.Text;

                connectionString = db.ConnectionString;

                _msg.Length = 0;
                _msg.Append("Connection string is: \r\n");
                _msg.Append(connectionString);
                Program._messageLog.WriteLine(_msg.ToString());

                if (_frm.txtSQLQuery.Text.Length == 0)
                {
                    throw new System.Exception("You must specify a SQL query to run.");
                }

                sw.Start();

                db.OpenConnection();

                db.SQLQuery = _frm.txtSQLQuery.Text;
                if (_frm.chkIsStoredProcedure.Checked)
                {
                    db.CommandType = CommandType.StoredProcedure;
                }
                else
                {
                    db.CommandType = CommandType.Text;
                }

                sw.Stop();
                _msg.Length = 0;
                _msg.Append("Open connection time: ");
                _msg.Append(sw.FormattedElapsedTime);
                Program._messageLog.WriteLine(_msg.ToString());


                Program._messageLog.WriteLine("\r\nRunning data extract tests ...\r\n");
                db.returnResultAsString += new PFSQLAnywhere.ResultAsStringDelegate(OutputResultsToFile);
                if (_textFile.FileIsOpen)
                {
                    _textFile.CloseFile();
                }
                _textFile.OpenFile(@"c:\temp\ReaderDelimitedTestExtract.txt", PFFileOpenOperation.OpenFileForWrite);
                sw.Start();
                SADataReader rdr = (SADataReader)db.RunQueryDataReader();
                db.ExtractDelimitedDataFromDataReader(rdr, ",", "\r\n", true);
                sw.Stop();
                _msg.Length = 0;
                _msg.Append("Extract Delimiated Dataset time: ");
                _msg.Append(sw.FormattedElapsedTime);
                Program._messageLog.WriteLine(_msg.ToString());

                if (_textFile.FileIsOpen)
                {
                    _textFile.CloseFile();
                }
                _textFile.OpenFile(@"c:\temp\ReaderFixedLengthTestExtract.txt", PFFileOpenOperation.OpenFileForWrite);
                rdr.Close();


                try
                {
                    sw.Start();
                    rdr = (SADataReader)db.RunQueryDataReader();
                    db.ExtractFixedLengthDataFromDataReader(rdr, true, true, false);
                    sw.Stop();
                    _msg.Length = 0;
                    _msg.Append("Extract Fixed Length Dataset time: ");
                    _msg.Append(sw.FormattedElapsedTime);
                    Program._messageLog.WriteLine(_msg.ToString());
                }
                catch (System.Exception ex)
                {
                    _msg.Length = 0;
                    _msg.Append("ERROR: Unable to create fixed width output file.");
                    _msg.Append(Environment.NewLine);
                    _msg.Append("Error Message:");
                    _msg.Append(Environment.NewLine);
                    _msg.Append(AppGlobals.AppMessages.FormatErrorMessage(ex));
                    Program._messageLog.WriteLine(_msg.ToString());
                    AppMessages.DisplayErrorMessage(_msg.ToString(), _saveErrorMessagesToAppLog);
                }
                finally
                {
                    if (_textFile.FileIsOpen)
                    {
                        _textFile.CloseFile();
                    }
                    rdr.Close();
                }



                rdr = (SADataReader)db.RunQueryDataReader();
                db.SaveDataReaderToXmlFile(rdr, @"c:\temp\Testrdr.xml");
                rdr.Close();
                rdr = (SADataReader)db.RunQueryDataReader();
                db.SaveDataReaderWithSchemaToXmlFile(rdr, @"c:\temp\Testrdrplus.xml");
                rdr.Close();
                rdr = (SADataReader)db.RunQueryDataReader();
                db.SaveDataReaderToXmlSchemaFile(rdr, @"c:\temp\Testrdr.xsd");
                rdr.Close();


                rdr = (SADataReader)db.RunQueryDataReader();
                PFDataProcessor dataProcessor = new PFDataProcessor();
                XmlDocument     xmlDoc        = dataProcessor.CopyDataTableToXmlDocument(db.ConvertDataReaderToDataTable(rdr));
                Program._messageLog.WriteLine("\r\n" + xmlDoc.OuterXml + "\r\n");
                rdr.Close();
            }
            catch (System.Exception ex)
            {
                _frm.OutputErrorMessageToLog(ex);
            }
            finally
            {
                db.CloseConnection();
                db = null;
            }
        }