Connecting: GNOME Data Access 5 manual

## Connecting

Libgda allows data sources (DSN) to be defined and referred to by a unique name which contains all the required information to actually open a connection (except the name and password if they are required); see the Configuration section for more information about how to manage data sources. Of course it's still possible to open a connection without having defined a DSN, in which case a connection string is used to specify all the parameters required to open a connection. For more information about connection strings, see the gda_connection_open_from_string ()'s documentation.

Connections are opened using gda_connection_open_from_dsn () for connections which are defined as a data source (DSN), or or gda_connection_open_from_string () otherwise. Each connection object can then be used to actually execute queries, for example:

    void
    do_stuff () {
    	GdaConnection *connection;

            /* open a connection */
    	g_print ("CONNECTING\n");
    	connection = gda_connection_open_from_dsn ("calvaris", NULL,
                                                       GDA_CONNECTION_OPTIONS_READ_ONLY, NULL);
    	if (!connection) {
    		g_print ("CONNECTION FAILED\n");
    		return;
    	}
    	g_print ("CONNECTED\n");

            /* use the connection */
    	execute_some_queries (connection);

            /* close the connection */
            g_object_unref (G_OBJECT (connection));
    }

Closing the connection can be ordered using gda_connection_close (), or is automatically done when the connection object is destroyed (as is the case in the example above when g_object_unref() is called with the connection as argument).