コード例 #1
0
ファイル: MainWindow.xaml.cs プロジェクト: newenglander/DB2GX
        private void serverSelectionChanged(object sender)
        {
            databases.Items.Clear();

            ArrayList sortedDBs = new ArrayList();

            String currentDBServer = textBox_Server.Text;
            String currentDBServerPort = "";

            if (textBox_Port.Text == "")
            {
                if (databaseServers.SelectedItem != null)
                    currentDBServerPort = ((ComboBoxServer)databaseServers.SelectedItem).Port;
                else
                    currentDBServerPort = textBox_Port.Text;
                textBox_Port.Text = currentDBServerPort;
            }
            else
                currentDBServerPort = textBox_Port.Text;

            TextBlockStatus.Text = "Verfügbare Datenbanken werden in " + currentDBServer + " gesucht.";

            if (comboBoxDBType.SelectedItem.ToString() == DBPOSTGRES)
            {
                this.databases.Items.Clear();
                this.hisProduct.Items.Clear();
                this.comboBoxEncoding.Items.Clear();

                DBConnection pgConnection = new DBConnection(DBConnection.DBType.Postgres);
                NpgsqlConnection sqlConx = (NpgsqlConnection)pgConnection.openPGConnection(currentDBServer, "postgres", currentDBServerPort, getHisProduct(), false);

                if (sqlConx == null)
                {
                    TextBlockStatus.Text = "Datenbank Verbindung fehlgeschlagen!";
                    return;
                }

                DataTable tblDatabases = sqlConx.GetSchema("Databases");
                DataView view = tblDatabases.DefaultView;
                view.Sort = "database_name";

                sqlConx.Close();

                foreach (DataRowView row in view)
                {
                    try
                    {
                        String dbName = row["database_name"].ToString().Trim();
                        String dbOwner = row["owner"].ToString().Trim();
                        String dbEncoding = row["encoding"].ToString().Trim();

                        if ((dbName != "postgres") && (dbName != "template0") && (dbName != "template1") && (dbName != "latin1"))
                        {
                            sortedDBs.Add(new ComboBoxDatabase(dbName, dbOwner, dbEncoding));
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error: " + ex.Message);
                        return;
                    }
                }
            }
            else if (comboBoxDBType.SelectedItem.ToString() == DBINFORMIX)
            {
                if (databaseServers.SelectedItem == null)
                    return;
                String currentInformixServer = ((ComboBoxServer)databaseServers.SelectedItem).InformixServer;

                if (!SQLHosts.EntryExists(currentInformixServer))
                {
                    SQLHosts.CreateEntry(currentInformixServer, currentDBServer, currentDBServerPort);
                }

                try
                {
                    DBConnection ifxConnection = new DBConnection(DBConnection.DBType.Informix);

                    IfxConnection conn = (IfxConnection)ifxConnection.openIfxConnection(currentDBServer, currentInformixServer, "sysmaster", currentDBServerPort, getHisProduct(), "", false);

                    if (conn == null)
                    {
                        TextBlockStatus.Text = "Datenbank Verbindung fehlgeschlagen!";
                        return;
                    }

                    String commandText = "SELECT sysdatabases.name, sysdatabases.owner, sysdbslocale.dbs_collate " +
                                         "FROM sysdbslocale INNER JOIN sysdatabases ON sysdatabases.name = sysdbslocale.dbs_dbsname " +
                                         "ORDER BY sysdatabases.name;";

                    IfxDataReader reader = (IfxDataReader)ifxConnection.readQuery(commandText);

                    // reader.HasRows was returning FALSE on some machines when there were rows returned
                    while ((reader != null) && reader.Read())
                    {
                        String dbName = reader[0].ToString().Trim();
                        String dbOwner = reader[1].ToString().Trim();
                        String dbEncoding = reader[2].ToString().Trim();
                        if (!dbName.StartsWith("sys"))
                        {
                            sortedDBs.Add(new ComboBoxDatabase(dbName, dbOwner, dbEncoding));
                        }
                    }

                    conn.Close();
                    conn.Dispose();

                }
                catch (IfxException ex)
                {
                    MessageBox.Show("Failed opening connection: " + ex);
                    TextBlockStatus.Text = "Datenbank Verbindung fehlgeschlagen!";
                    return;
                }
                catch (TypeInitializationException ex)
                {
                    MessageBox.Show("Failed opening connection: " + ex);
                    TextBlockStatus.Text = "Datenbank Verbindung fehlgeschlagen!";
                    return;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Failed opening connection: " + ex);
                    TextBlockStatus.Text = "Datenbank Verbindung fehlgeschlagen!";
                    return;
                }

            }

            this.databases.Items.Clear();
            foreach (ComboBoxDatabase db in sortedDBs)
            {
                this.databases.Items.Add(db);
            }
            // filter when called normally
            if (sender != null)
                textBox_filterDBs_TextChanged(null, null);
            TextBlockStatus.Text = "Server " + currentDBServer + " hat " + databases.Items.Count + " Datenbanken verfügbar.";
        }
コード例 #2
0
ファイル: MainWindow.xaml.cs プロジェクト: newenglander/DB2GX
        private DBConnection DBConnectionSetup()
        {
            if ((databases.SelectedItem == null) || ((databaseServers.SelectedItem == null) && (this.textBox_Server.Text == "")))
                return null;
            String dbServerName = textBox_Server.Text;
            String dbServerPort = textBox_Port.Text;
            String dbName = ((ComboBoxDatabase)(databases.SelectedItem)).Name;

            DBConnection dbconn = new DBConnection((comboBoxDBType.SelectedItem.ToString() == DBPOSTGRES) ? DBConnection.DBType.Postgres : DBConnection.DBType.Informix);

            if (comboBoxDBType.SelectedItem.ToString() == DBPOSTGRES)
            {
                dbconn.openPGConnection(dbServerName, dbName, dbServerPort, getHisProduct(), false);
            }
            else
            {
                String ifxServerName = ((ComboBoxServer)(databaseServers.SelectedItem)).InformixServer;
                String currentInformixLocale = ((ComboBoxDatabase)databases.SelectedItem).Encoding;
                dbconn.openIfxConnection(dbServerName, ifxServerName, dbName, dbServerPort, getHisProduct(), currentInformixLocale, false);
            }

            return dbconn;
        }
コード例 #3
0
ファイル: MainWindow.xaml.cs プロジェクト: newenglander/DB2GX
        private void findUserDBs(object sender, DoWorkEventArgs e)
        {
            Thread.CurrentThread.Priority = ThreadPriority.Lowest;

            BackgroundWorker worker = sender as BackgroundWorker;

            Dispatcher.Invoke(new Action(() => { databaseServers_Loaded(null, null); }));

            List<Win32.NetApi32.SERVER_INFO_101> serverList = Win32.NetApi32.GetServerList(Win32.NetApi32.SV_101_TYPES.SV_TYPE_ALL);

            foreach (Win32.NetApi32.SERVER_INFO_101 server in serverList)
            {
                if (worker.CancellationPending) break;

                String nameToShow = server.sv101_comment;
                if (nameToShow.Contains("UB1"))
                {
                    // this test takes too long
                    DBConnection pgConnection = new DBConnection(DBConnection.DBType.Postgres);

                    if (pgConnection.openPGConnection(server.sv101_name, "postgres", textBox_Port.Text, "", true) != null)
                    {
                        nameToShow = nameToShow.Replace("UB1", "");
                        nameToShow = nameToShow.Trim('-', ' ');

                        Dispatcher.Invoke(new Action(() => { databaseServers.Items.Add(new ComboBoxServer(server.sv101_name, textBox_Port.Text, nameToShow)); }), System.Windows.Threading.DispatcherPriority.Background, null);

                        pgConnection.closeConnection();
                    }
                }
            }
        }
コード例 #4
0
ファイル: MainWindow.xaml.cs プロジェクト: newenglander/DB2GX
        private void createNewEntry()
        {
            if ((comboBoxDBType.SelectedIndex == -1) || ((databaseServers.SelectedIndex == -1) && textBox_Server.Text == "")||
                (databases.SelectedIndex == -1) || (hisProduct.SelectedIndex == -1) ||
                (comboBoxEncoding.SelectedIndex == -1) || (textBoxConnectionName.Text.Trim() == ""))
            {
                MessageBox.Show("Fehlende Eingabe!");
                return;
            }

            try
            {
                String setSearchPathTo = "";
                String dbName = ((ComboBoxDatabase)(databases.SelectedItem)).Name;
                String dbServerName = textBox_Server.Text;
                String dbServerPort = textBox_Port.Text;
                String hisProductName = getHisProduct();
                String entryName = textBoxConnectionName.Text.Trim();

                if (entryName.Length > 30)
                {
                    MessageBoxResult res = MessageBox.Show("Name zu lang!");
                    return;
                }
                if (ODBCManager.DSNExists(entryName))
                {
                    MessageBoxResult res = MessageBox.Show("ODBC Eintrag " + entryName + " exisitiert schon! Fortfahren?", "Warnung", MessageBoxButton.YesNo);
                    if (res == MessageBoxResult.No) return;
                }

                if (RegistryManager.EntryExists(hisProductName, entryName, dbServerName))
                {
                    MessageBoxResult res = MessageBox.Show("Registry Eintrag " + dbName + " exisitiert schon! Fortfahren?", "Warnung", MessageBoxButton.YesNo);
                    if (res == MessageBoxResult.No) return;
                }

                if (comboBoxDBType.SelectedItem.ToString() == DBPOSTGRES)
                {

                    // create odbc connection
                    DBConnection pgConnection = new DBConnection(DBConnection.DBType.Postgres);
                    NpgsqlConnection con = (NpgsqlConnection)pgConnection.openPGConnection(dbServerName, dbName, dbServerPort, getHisProduct(), false);
                    // if a namespace 'mbs' exists, then we have a hisrm database and need to set the search path
                    String query = "SELECT COUNT(*) FROM pg_catalog.pg_namespace WHERE nspname IN ('mbs', 'sva4')";

                    NpgsqlCommand command = new NpgsqlCommand(query, con);
                    NpgsqlDataReader reader = command.ExecuteReader();
                    reader.Read();
                    if (int.Parse(reader[0].ToString()) > 0)
                    {
                        if ((hisProduct.SelectedItem.ToString().Contains(HISMBSGX)) || (hisProduct.SelectedItem.ToString().Contains(HISFSVGX)))
                            setSearchPathTo = "mbs";
                        else if (hisProduct.SelectedItem.ToString().Contains(HISSVAGX))
                            setSearchPathTo = "sva4";
                        else if (hisProduct.SelectedItem.ToString().Contains(HISCOBGX))
                            setSearchPathTo = "cob";
                    }
                    reader.Close();
                    string user = "";
                    pgConnection.createUserAndPasswordString(getHisProduct(), ref user);
                    String driverName;

                    if (comboBoxEncoding.SelectedItem.ToString() == PGANSI)
                        driverName = PGANSI;
                    else
                        driverName = PGUNICODE;
                    bool retval = ODBCManager.CreateDSN(entryName, dbServerName, driverName,
                                                        true, dbName, dbServerPort, user, setSearchPathTo);

                    if (!retval)
                    {
                        TextBlockStatus.Text = "fail: ODBCManager.CreateDSN";
                        return;
                    }

                    // add lang to db, if necessary
                    command = new NpgsqlCommand(@"CREATE OR REPLACE FUNCTION make_plpgsql()
                                                            RETURNS VOID
                                                            LANGUAGE SQL
                                                            AS $$
                                                             CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
                                                             HANDLER plpgsql_call_handler
                                                             VALIDATOR plpgsql_validator;
                                                             ALTER LANGUAGE plpgsql OWNER TO postgres;
                                                            $$;
                                                            SELECT
                                                                CASE
                                                                WHEN EXISTS(
                                                                    SELECT 1
                                                                    FROM pg_catalog.pg_language
                                                                    WHERE lanname='plpgsql'
                                                                )
                                                                THEN NULL
                                                                ELSE make_plpgsql() END;
                                                            DROP FUNCTION make_plpgsql();", con);
                    int returnCode = command.ExecuteNonQuery();

                    con.Close();

                }
                else if (comboBoxDBType.SelectedItem.ToString() == DBINFORMIX)
                {
                    String host = dbServerName;
                    dbServerName = ((ComboBoxServer)(databaseServers.SelectedItem)).InformixServer;
                }

                // create registry entries
                RegistryManager.CreateEntry(hisProductName, entryName, dbServerName, dbName, comboBoxDBType.SelectedItem.ToString());

                TextBlockStatus.Text = "Datenbank " + dbName + " für die Benutzung mit GX erfolgreich eingerichtet; Verfügbar über Eintrag " + entryName + ".";
                MessageBox.Show(TextBlockStatus.Text, "Erfolgreich eingerichtet");
                // reload list for immediate deletion of new database
                comboBox_delete_Loaded(null, null);
            }
            catch (Exception ex)
            {
                TextBlockStatus.Text = ex.Message;
                return;
            }
        }