Reputation:
I have PostgresQL database, and I develop interface application using C# and Npgsql to connect to the database, how can I assign the Npgsql connection to TableAdapter?
Thanks,
Upvotes: 4
Views: 8559
Reputation: 11
Sorry its a bit late however here is how you would do it, use the data adapter.
create a datagrid create a datatable
fill the datatable with the dataAdapter, // function is provided - dataTableName.fill() set the datagrid to display default view of the dataTable
try
{
using (var Connection = new NpgsqlConnection(PG_Connection_String))
NpgsqlDataAdapter da = new NpgsqlDataAdapter("myQuery", connectionString))
{
Connection.Open();
myTable = new System.Data.DataTable();
da.Fill(myTable);
postgresql_dataGrid.DataSource = myTable.DefaultView;
Connection.Close();
}
}
catch (Exception Ex)
{
MessageBox.Show("Your Error", "Connection Error");
}
}
Bob's your uncle, I'm using it successfully. I would suggest that if you wish to prepare the grid for custom headers etc, do it BEFORE setting the default view of your datagrid
Upvotes: 1
Reputation: 4471
I'm in the exact same boat with PostgreSQL.
I don't believe that's possible. Npgsql has its own data adapter (see http://npgsql.projects.postgresql.org/docs/manual/UserManual.html and search for text "adapter"). The downside to this is that you can't use the Visual Studio Designer.
So instead, I'm using .NET's ODBC DataSource. For this to work, you will need to install the postgresql odbc driver, which is available here: http://www.postgresql.org/ftp/odbc/versions/msi/. After installing, you can go to Control Panel -> Administrative Tools -> Data Soruces (ODBC) to add a DSN (Data Source Name). Finally, in Visual Studio go to the Server Explorer, right click "Data Connections" and select "Add Connection...", and change the data source of Microsoft ODBC Data Source. Here you can select the DSN you provided earlier, and viola! You're in business.
(Note that for some crazy reason bools come in as strings. You can change this in the ODBC Data Source Administrator by clicking "Configure" on your PostgreSQL datasource, going to Datasource options, and unchecking "Bools as Char".)
Upvotes: 1