Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: mono-1-1-10
Fetching contributors…

Cannot retrieve contributors at this time

executable file 157 lines (124 sloc) 6.498 kB
* Provider Factory
<p>Brian Ritchie contributed a Provider Factory and Data Tools for Mono ADO.NET<br>
which gives us a foundation for abstract data provider access within Mono
** Here are the deails:
<ul>
<li>The Provider information is seperate from the connection string information.<br>
This allows the list of providers to be stored in the machine.config file.</li>
<li>Provider and ProviderCollection objects are available to access <br>
the list of providers and modify them at runtime.</li>
<li>The ProviderFactory object is used to create new connections, commands,<br>
dataadapters, parameters, etc.</li>
</ul>
** Overview of the ProviderFactory object model:
<ul>
<li><b>ProviderFactory</b>: used to create new Connections, Commands,<br>
DataAdapters, or Commands. All objects are returned using <br>
the provider interfaces such as IDbConnection, IDbCommand,<br>
IDbDataAdapter, or IDataParamter</li>
<li><b>DataTools</b>: static methods for doing <br>
common tasks like filling a DataSet <br>
with the contents of a select statement.</li>
<li><b>ProviderCollection</b>: list of providers configured <br>
in the system. Initially loaded from app.config, but can <br>
be modified at run-time.</li>
<li><b>Provider</b>: represents a given provider (factory) <br>
and holds information needed to <br>
create the types.</li>
<li><b>ProviderSectionHandler</b>: works behind the <br>
scenes to load the list of <br>
providers from the app.config into a ProviderCollection.</li>
</ul>
** C# source code samples for creating a connection:
<pre>
// 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 &lt;appSettings&gt;
IDbConnection conn;
string appSetting = "PubsConnStr";
conn = ProviderFactory.CreateConnectionFromConfig(appSetting);
</pre>
<p>C# Sample for Creating a DataAdapter and filling a DataSet.
<pre>
// 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");
</pre>
<p>Creating a DataAdapter and filling a DataSet. <br>
The super lazy method for people like me.
<pre>
// Create Connection
IDbConnection conn =
ProviderFactory.CreateConnectionFromConfig("TdsPubsConnStr");
// Data Adapter
DataSet ds=DataTools.FillDataSet(conn, "select * from author");
</pre>
<p>Here's some sample code on displaying a list <br>
of configured ADO.NET providers:
<pre>
Console.WriteLine("Configured Providers:");
foreach (Provider p in ProviderFactory.Providers)
Console.WriteLine(p.Description);
</pre>
<p>A Super lazy overload to the FillDataSet method (in <br>
DataTools.cs) that will fill a dataset in one line of code.
<pre>
DataSet ds=DataTools.FillDataSet("PubsConnStr", "select * from authors");
</pre>
** About Configuration Files
<p>Information about <a href="http://msdn.microsoft.com/library/en-us/vbcon/html/vboriintroductiontoapplicationsettingstorage.asp?frame=true">app.config</a> files
can be found at <a href="http://msdn.microsoft.com/">MSDN</a>.
<p>See the mcs/class/Mono.Data/app.config for sample configuration file in<br>
mcs source. Basically, if your application is named blah.exe, <br>
you would create an app.config file named blah.exe.config
<p>Here's a sample app.config file showing the provider <br>
declarations along with sample connection strings:
<pre>
&lt;?xml version="1.0" encoding="utf-8" ?&gt;
&lt;configuration&gt;
&lt;configSections&gt;
&lt;sectionGroup name="mono.data"&gt;
&lt;section name="providers" type="Mono.Data.ProviderSectionHandler,Mono.Data" /&gt;
&lt;/sectionGroup&gt;
&lt;/configSections&gt;
&lt;appSettings&gt;
&lt;add key="PubsConnStr" value="factory=System.Data.SqlClient;server=speedy;database=pubs;uid=sa;pwd=" /&gt;
&lt;/appSettings&gt;
&lt;mono.data&gt;
&lt;providers&gt;
&lt;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"/&gt;
&lt;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"/&gt;
&lt;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"/&gt;
&lt;provider name="Mono.Data.TdsClient" connection="Mono.Data.TdsClient.TdsConnection" adapter="Mono.Data.TdsClient.TdsDataAdapter" assembly="Mono.Data.TdsClient" /&gt;
&lt;provider name="Mono.Data.MySql" connection="Mono.Data.MySql.MySqlConnection" adapter="Mono.Data.MySql.MySqlDataAdapter" assembly="Mono.Data.MySql" /&gt;
&lt;provider name="Mono.Data.PostgreSqlClient" connection="Mono.Data.PostgreSqlClient.PgSqlConnection" adapter="Mono.Data.PostgreSqlClient.PgSqlDataAdapter" assembly="Mono.Data.PostgreSqlClient" /&gt;
&lt;provider name="Mono.Data.SqliteClient" connection="Mono.Data.SqliteClient.SqliteConnection" adapter="Mono.Data.SqliteClient.SqliteDataAdapter" assembly="Mono.Data.SqliteClient" /&gt;
&lt;provider name="Mono.Data.SybaseClient" connection="Mono.Data.SybaseClient.SybaseConnection" adapter="Mono.Data.SybaseClient.SybaseDataAdapter" assembly="Mono.Data.SybaseClient" /&gt;
&lt;/providers&gt;
&lt;/mono.data&gt;
&lt;/configuration&gt;
</pre>
Jump to Line
Something went wrong with that request. Please try again.