Sunday, November 12, 2006

Initial Npgsql2 provider factory support added to cvs

UPDATE: Thomas Burkhart sent me a mail telling me that this configuration syntax doesn't work on ms.net 2.0. This is the version which works:

<?xml version="1.0" encoding="iso-8859-1" ?>
<configuration>

<system.data>
<DbProviderFactories>

<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7" />

</DbProviderFactories>
</system.data>

</configuration>


Thomas said that "the tags are case sensitive and the must be outside any <applicationSettings>"


Thank you Thomas for your feedback which, for sure will help others to get their configuration up and running. I will check with Mono guys to see why my original configuration worked.




Hi all!

Today I committed an initial support for provider factory to Npgsql2.

This will allow ado.net 2.0 users to work with Npgsql using ado.net generic coding.

In order to use it, you have to add the following xml config in your machine.config or yourapp.exe.config file:


<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
<appsettings>

<system.data>
<dbproviderfactories>

<add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql Server" type="Npgsql.NpgsqlFactory, Npgsql, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7">
</add>
</dbproviderfactories>
</system.data>
</appsettings>
</configuration>


P.S.: Sorry for indentation lacking. I don't know how to add it on blogger :(

Note that this is the full contents of a yourapp.exe.config file. If you are changing the machine.config file, just add the "add name=..." section.

The key elements here are the invariant name which will be used in your application when returning the provider factory. In this case it is defined as "Npgsql". But you can specify whatever you want.


That's it! If everything is ok, you will be able to write some code like that, to connect to postgresql:


using System;
using System.Data;
using System.Data.Common;


public class c
{
public static void Main(String[] args)
{

DbProviderFactory factory = DbProviderFactories.GetFactory("Npgsql");

DbConnection conn = factory.CreateConnection();

conn.ConnectionString = "Server=127.0.0.1;User id=npgsql_tests;password=npgsql_tests;";

conn.Open();

conn.Close();


}
}


Enjoy!


P.S.: Npgsql2 code is found in Npgsql2 cvs module. See here for more info about how to get it.

Warning: This is a very early code, so bugs still exist and there are things missing. Basic functionality should work ok.