简体   繁体   中英

Add PostgreSQL databaseas data source for WinForms DataGridView

I've successfully connected to a PostgreSQL database by manually coding it (create connection string for IP address, port, credentials and database name, create the NpgsqlConnection object, and open the connection).

Now I need to add that database as a Data Source for a DataGridView in a WinForms project. I ran across Devart's dotConnection for PostgreSQL and downloaded the Express version. They have a documentation page , but for the life of me I can't figure out how to add the database as a datasource (I also reached out to their support email three days ago, but they've never responded.)

When I click Add New Data Source in the Data Sources tab and the Data Source Configuration Wizard opens, I'm not sure if I should select Database or Object. In any case, I'm not seeing how to add the PostgreSQL database connection information as a datasource through the wizard.

I believe the best way to do that is not to use the wizards, they hide code in .Designer.cs and .resx files and make maintenance difficult.

I recommend you do something like this programmatically:

DataGridView1.DataSource = GetData("Your sql");

public DataTable GetData(string selectSql)
{
    try
    {
        DataSet ds = new DataSet();             
        string connstring = String.Format("Your conn string");

        NpgsqlConnection conn = new NpgsqlConnection(connstring);
        conn.Open();
        NpgsqlDataAdapter da = new NpgsqlDataAdapter(selectSql, conn);         
        da.Fill(ds);                                                           
        return ds.Tables[0];
    }
    finally
    {
        conn.Close();
    }                                                      
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM