Connection with two banks in c#

Asked

Viewed 65 times

0

I need to connect to two types of databases, namely: Oracle and Postegre. I need to go to Oracle, generate a spool with some queries and upload this spool in Postegre. Is it possible to do this in C#? I’m having a lot of difficulty finding content related to this theme in forum etc.. Can this code for connection ORACLE:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace aplicacao_fatura_devolvida
{
    class ConsultaBancoOra
    {
        //public Process Sqlpl { get; private set; }
        private string connection = "Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)" +
            "(HOST=xx.xx.xx.xxx)(PORT=xxxx))) (CONNECT_DATA=(SERVICE_NAME=xx))); User Id=xxxx; Password=xxxxxx";
        //Consulta ORACLE
        [Obsolete]
        public ConsultaBancoOra()
        {
            //String de Conexão
            OracleConnection ora = new OracleConnection(connection);
            //OracleCommand oraComand = new OracleCommand();
            //oraComand = ora.CreateCommand();
            //oraComand.CommandText = " select * from tb_003_ref_fd_dados_carga_base;";
            //oraComand.CommandTimeout = 15;
            //oraComand.CommandType = CommandType.Text;
            MessageBox.Show("Conexão realizada");
        }
        [Obsolete]
        public void AbrirConexaoOra()
        {
            OracleConnection ora = new OracleConnection(connection);
            ora.Open();
        }
        [Obsolete]
        public void FechaConexaoOra()
        {
            OracleConnection ora = new OracleConnection(connection);
            ora.Close();
        }
        public void ExecuteScriptOra(string Script, bool sequencial)
        {
            try
            {
                using (Process process = new Process())
                {
                    process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
                    process.StartInfo = new ProcessStartInfo("Sqlplus", $@" -d "" host=xx.xx.xx.xxxx port=xxxxdbname=xxxxx user=xxxxpassword=xxxxx"" " + Script);
                    //process.StandardInput.WriteLine();
                    process.StartInfo.UseShellExecute = false;
                    process.Start();
                    if (sequencial)
                    {
                        process.WaitForExit();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        
        }
    }
}
  • Yes, it is possible. Do the query normally in Oracle and "equally normally" do the Insert in Postgres. There is nothing to mix the themes. However, possibly you would not do this with SQL, but besides not being known your use case, would be outside the scope of the question. If the goal is to synchronize the 2, you can try tools like debezium.io.

  • Thanks for the feedback. In fact, I need to create in C# a button in Forms, this button will generate a spool with the tables I will mention and the postegre will take these spool files and upload the updated file. I’m not getting out of this obstacle, I was able to connect with postegre, but with oracle not..

  • create two Connections, one with Oracle and the other with Postgree, this is simple and has many examples, then make the query using the Oracle Connection and what you need with the data in the other Connection. Ask the question what you already have of code and where are the doubts

  • Oops, Ricardo. All right? I got connection with Postegre, but with Oracle I’m having difficulties, but I’m seeing examples and some videos..

  • @Ricardopunctual attached my oracle connection in the publication for better understanding

  • there’s a lot of weird stuff in there, for example method FechaConexaoOra why open to close then? avoid this, in the method where cosulta, open the connection inside a block using (var conexao = new OracleConnection(connection)). But what’s the mistake? put this on a Try/catch to see what the problem is. Remember that depending on the connection, Oracle needs the name to be in that file tnsnames.ora if I’m not mistaken. Here are some examples of strings: https://www.c-sharpcorner.com/UploadFile/nipuntomar/connection-strings-for-oracle/

  • Thank you, @Ricardopunctual. ..

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.