1

I want to connect to the tableau PostgreSQL server from my .Net framework to list all the reports and datasources published in the tableau server.

For doing this, I have done the following steps.

  1. Added the npgsql.dll reference that i downloaded online
  2. Added the below two namespaces in my class file

    using NpgsqlTypes; using Npgsql;

  3. I added the connection sting as follows

I also tried with modifying the connection string with port value and renaming the DataSource to Server, Initial catalog to Database and provider Name to Npgsqll

  1. My Method is as follows:

    public DataTable  getAllDataSourceNames()
    {
        DataTable dataSourceNames = new DataTable();
        NpgsqlConnection conServer = new NpgsqlConnection(conString);
        conServer.Open();
        string command = @"select * from datasources";
        NpgsqlDataAdapter sqlcmd = new NpgsqlDataAdapter(command,conServer);
        sqlcmd.Fill(dataSourceNames);
        return dataSourceNames;
    }`
    
  2. No error. I can build and run successfully the other links in the website. But cannot cannot establish connection to my postgresql server.

Any idea of how to establish the connection?

4
  • Where is this conString came from? Commented Oct 24, 2017 at 6:58
  • I would suggest you check the tableau community, like this post. By default, the database did not open for outside connection. Commented Oct 24, 2017 at 7:58
  • @reds I got the connection string properly. Commented Oct 24, 2017 at 8:45
  • public string conString = System.Configuration.ConfigurationManager.ConnectionStrings["Tableau_Details"].ConnectionString.ToString(); Commented Oct 24, 2017 at 8:45

2 Answers 2

1

Working with Postgres Connection in c#:

private DataSet ds = new DataSet();
private DataTable dt = new DataTable();
public Form1()
{    
    InitializeComponent();    
}
private void llOpenConnAndSelect_LinkClicked(object sender, 
            LinkLabelLinkClickedEventArgs e)
{
    try
    {
        // PostgeSQL-style connection string
        string connstring = String.Format("Server={0};Port={1};" + 
            "User Id={2};Password={3};Database={4};",
            tbHost.Text, tbPort.Text, tbUser.Text, 
            tbPass.Text, tbDataBaseName.Text );
        // Making connection with Npgsql provider
        NpgsqlConnection conn = new NpgsqlConnection(connstring);
        conn.Open();
        // quite complex sql statement
        string sql = "SELECT * FROM simple_table";
        // data adapter making request from our connection
        NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, conn);
        // i always reset DataSet before i do
        // something with it.... i don't know why :-)
        ds.Reset();
        // filling DataSet with result from NpgsqlDataAdapter
        da.Fill(ds);
        // since it C# DataSet can handle multiple tables, we will select first
        dt = ds.Tables[0];
        // connect grid to DataTable
        dataGridView1.DataSource = dt;
        // since we only showing the result we don't need connection anymore
        conn.Close();
    }
    catch (Exception msg)
    {
        // something went wrong, and you wanna know why
        MessageBox.Show(msg.ToString());
        throw;
    }
}

The following link may help you: Using PostgreSQL in your C# .NET application

Sign up to request clarification or add additional context in comments.

6 Comments

This should be a comment or contain a little brief of the link you have posted.
How about that @Isma
Yes I tried it. Am getting IOException "Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host" Any idea how I can handle it ?
That's much better @reds ;-)
Try to check your Server to DB if Connected or not.. much better to do it on cmd like Ping Command. Also check your DB Username access previlege.
|
0

Short answer -- no, not sure what's wrong. Your code doesn't raise any alarm bells.

Somewhat related, and it may help: I'm a big fan of the Connection StringBuilder with Npgsql. Here is a brief example:

NpgsqlConnectionStringBuilder sb = new NpgsqlConnectionStringBuilder();
sb.ApplicationName = "Tableau " + Environment.GetEvironmentVariable("USERNAME");
sb.Host = "1.2.3.4";
sb.Port = 5432;
sb.Username = "foo";
sb.Password = "bar";
sb.Database = "postgres";
sb.Pooling = false;
sb.Timeout = 120;

conServer = new NpgsqlConnection(sb.ToString());

It demistifies all of this and makes injecting parameters easy. I highly recommend you add the ApplicationName property so that when you are monitoring sessions, you will know who is who.

2 Comments

I dont have ApplicationName parameter in NpgsqlConnectionStringBuilder class. and when i provide without specifying the application name. am still getting the same IOException @Hambone
The ApplicationName parameter isn't mandatory at all, but it's helpful. But the fact that it's missing hints that maybe you have a really old version of NpgSql. What versions are NpgSql and your PostgreSQL server?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.