* Provider Factory
Brian Ritchie contributed a Provider Factory and Data Tools for Mono ADO.NET
which gives us a foundation for abstract data provider access within Mono
** Here are the deails:
// Create connection using enhanced connection string // The factory attribute specifies which provider // to use. The factory attribute is parsed out, the // object is created, and then the rest of the // connection string is passed into the provider. The // providers are defined in // the app.config (or machine.config). IDbConnection conn; string connectionString = "factory=System.Data.SqlClient;" + "server=speedy;database=pubs;uid=sa"; conn = ProviderFactory.CreateConnection(connectionString); // Create connection specifying provider // and standard connection string IDbConnection conn; string provider = "System.Data.SqlClient"; string connectionString = "server=speedy;database=pubs;uid=sa"; conn = ProviderFactory.CreateConnection(provider,connectionString); // Create connection using connection string stored // in app.config under <appSettings> IDbConnection conn; string appSetting = "PubsConnStr"; conn = ProviderFactory.CreateConnectionFromConfig(appSetting);
C# Sample for Creating a DataAdapter and filling a DataSet.
// Create Connection IDbConnection conn = ProviderFactory.CreateConnectionFromConfig("PubsConnStr"); // Select command IDbCommand cmd=conn.CreateCommand(); cmd.Text="select * from author"; // Data Adapter DataSet ds=new DataSet(); IDbDataAdapter adapter=ProviderFactory.CreateDataAdapter(cmd); adapter.Fill(ds, "Table1");
Creating a DataAdapter and filling a DataSet.
The super lazy method for people like me.
// Create Connection IDbConnection conn = ProviderFactory.CreateConnectionFromConfig("TdsPubsConnStr"); // Data Adapter DataSet ds=DataTools.FillDataSet(conn, "select * from author");
Here's some sample code on displaying a list
of configured ADO.NET providers:
Console.WriteLine("Configured Providers:"); foreach (Provider p in ProviderFactory.Providers) Console.WriteLine(p.Description);
A Super lazy overload to the FillDataSet method (in
DataTools.cs) that will fill a dataset in one line of code.
DataSet ds=DataTools.FillDataSet("PubsConnStr", "select * from authors");** About Configuration Files
Information about app.config files can be found at MSDN.
See the mcs/class/Mono.Data/app.config for sample configuration file in
mcs source. Basically, if your application is named blah.exe,
you would create an app.config file named blah.exe.config
Here's a sample app.config file showing the provider
declarations along with sample connection strings:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <sectionGroup name="mono.data"> <section name="providers" type="Mono.Data.ProviderSectionHandler,Mono.Data" /> </sectionGroup> </configSections> <appSettings> <add key="PubsConnStr" value="factory=System.Data.SqlClient;server=speedy;database=pubs;uid=sa;pwd=" /> </appSettings> <mono.data> <providers> <provider name="System.Data.SqlClient" connection="System.Data.SqlClient.SqlConnection" adapter="System.Data.SqlClient.SqlDataAdapter" assembly="System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <provider name="System.Data.OleDb" connection="System.Data.OleDb.OleDbConnection" adapter="System.Data.OleDb.OleDbDataAdapter" assembly="System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <provider name="System.Data.Odbc" connection="System.Data.Odbc.OdbcConnection" adapter="System.Data.OleDb.OdbcDataAdapter" assembly="System.Data, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/> <provider name="Mono.Data.TdsClient" connection="Mono.Data.TdsClient.TdsConnection" adapter="Mono.Data.TdsClient.TdsDataAdapter" assembly="Mono.Data.TdsClient" /> <provider name="Mono.Data.MySql" connection="Mono.Data.MySql.MySqlConnection" adapter="Mono.Data.MySql.MySqlDataAdapter" assembly="Mono.Data.MySql" /> <provider name="Mono.Data.PostgreSqlClient" connection="Mono.Data.PostgreSqlClient.PgSqlConnection" adapter="Mono.Data.PostgreSqlClient.PgSqlDataAdapter" assembly="Mono.Data.PostgreSqlClient" /> <provider name="Mono.Data.SqliteClient" connection="Mono.Data.SqliteClient.SqliteConnection" adapter="Mono.Data.SqliteClient.SqliteDataAdapter" assembly="Mono.Data.SqliteClient" /> <provider name="Mono.Data.SybaseClient" connection="Mono.Data.SybaseClient.SybaseConnection" adapter="Mono.Data.SybaseClient.SybaseDataAdapter" assembly="Mono.Data.SybaseClient" /> </providers> </mono.data> </configuration>