2010-04-16 Sebastien Pouliot <sebastien@ximian.com>
[mono/afaerber.git] / web / provider-factory
blob37309ec4324e1aa0ad7e9c22954ff6a7a2331268
1 * Provider Factory
3 <p>Brian Ritchie contributed a Provider Factory and Data Tools for Mono ADO.NET<br>
4 which gives us a foundation for abstract data provider access within Mono
6 ** Here are the deails:
8 <ul>
9         <li>The Provider information is seperate from the connection string information.<br>
10             This allows the list of providers to be stored in the machine.config file.</li>
11         <li>Provider and ProviderCollection objects are available to access <br>
12                 the list of providers and modify them at runtime.</li>
13         <li>The ProviderFactory object is used to create new connections, commands,<br> 
14             dataadapters, parameters, etc.</li>
15 </ul>
17 ** Overview of the ProviderFactory object model:
19 <ul>
20         <li><b>ProviderFactory</b>: used to create new Connections, Commands,<br> 
21         DataAdapters, or Commands. All objects are returned using <br>
22         the provider interfaces such as IDbConnection, IDbCommand,<br>
23         IDbDataAdapter, or IDataParamter</li>
25         <li><b>DataTools</b>: static methods for doing <br>
26         common tasks like filling a DataSet <br>
27         with the contents of a select statement.</li>
29         <li><b>ProviderCollection</b>: list of providers configured <br>
30         in the system. Initially loaded from app.config, but can <br>
31         be modified at run-time.</li>
33         <li><b>Provider</b>: represents a given provider (factory) <br>
34         and holds information needed to <br>
35         create the types.</li>
37         <li><b>ProviderSectionHandler</b>: works behind the <br>
38         scenes to load the list of <br>
39         providers from the app.config into a ProviderCollection.</li>
40 </ul>
42 ** C# source code samples for creating a connection: 
44 <pre>
45  // Create connection using enhanced connection string
46  // The factory attribute specifies which provider 
47  // to use. The factory attribute is parsed out, the 
48  // object is created, and then the rest of the 
49  // connection string is passed into the provider. The 
50  // providers are defined in 
51  // the app.config (or machine.config).
52  IDbConnection conn;
53  string connectionString = 
54         "factory=System.Data.SqlClient;" +
55         "server=speedy;database=pubs;uid=sa";
56  conn = ProviderFactory.CreateConnection(connectionString); 
58  // Create connection specifying provider
59  // and standard connection string
60  IDbConnection conn;
61  string provider = "System.Data.SqlClient";
62  string connectionString = "server=speedy;database=pubs;uid=sa";
63  conn = ProviderFactory.CreateConnection(provider,connectionString); 
65  // Create connection using connection string stored 
66  // in app.config under &lt;appSettings&gt;
67  IDbConnection conn;
68  string appSetting = "PubsConnStr";
69  conn = ProviderFactory.CreateConnectionFromConfig(appSetting); 
70 </pre>
73 <p>C# Sample for Creating a DataAdapter and filling a DataSet.
75 <pre>
76 // Create Connection
77 IDbConnection conn =
78 ProviderFactory.CreateConnectionFromConfig("PubsConnStr");
80 // Select command
81 IDbCommand cmd=conn.CreateCommand();
82 cmd.Text="select * from author";
84 // Data Adapter
85 DataSet ds=new DataSet();
86 IDbDataAdapter adapter=ProviderFactory.CreateDataAdapter(cmd);
87 adapter.Fill(ds, "Table1");
88 </pre>
90 <p>Creating a DataAdapter and filling a DataSet. <br>
91 The super lazy method for people like me.
92 <pre>
93 // Create Connection
94 IDbConnection conn =
95 ProviderFactory.CreateConnectionFromConfig("TdsPubsConnStr");
97 // Data Adapter
98 DataSet ds=DataTools.FillDataSet(conn, "select * from author");
99 </pre>
102 <p>Here's some sample code on displaying a list <br>
103    of configured ADO.NET providers:
104 <pre>
105 Console.WriteLine("Configured Providers:");
106 foreach (Provider p in ProviderFactory.Providers)
107         Console.WriteLine(p.Description);
108 </pre>
110 <p>A Super lazy overload to the FillDataSet method (in <br>
111 DataTools.cs) that will fill a dataset in one line of code.
113 <pre>
114 DataSet ds=DataTools.FillDataSet("PubsConnStr", "select * from authors");
115 </pre>
117 ** About Configuration Files
119 <p>Information about <a href="http://msdn.microsoft.com/library/en-us/vbcon/html/vboriintroductiontoapplicationsettingstorage.asp?frame=true">app.config</a> files
120 can be found at <a href="http://msdn.microsoft.com/">MSDN</a>.
122 <p>See the mcs/class/Mono.Data/app.config for sample configuration file in<br>
123 mcs source.  Basically, if your application is named blah.exe, <br>
124 you would create an app.config file named blah.exe.config
126 <p>Here's a sample app.config file showing the provider <br>
127 declarations along with sample connection strings:
129 <pre>
131 &lt;?xml version="1.0" encoding="utf-8" ?&gt;
132 &lt;configuration&gt;
133         &lt;configSections&gt;
134                 &lt;sectionGroup name="mono.data"&gt;
135                         &lt;section name="providers" type="Mono.Data.ProviderSectionHandler,Mono.Data" /&gt;
136                 &lt;/sectionGroup&gt;
137         &lt;/configSections&gt;
138         &lt;appSettings&gt;
139                 &lt;add key="PubsConnStr" value="factory=System.Data.SqlClient;server=speedy;database=pubs;uid=sa;pwd=" /&gt;
140         &lt;/appSettings&gt;
141         &lt;mono.data&gt;
142                 &lt;providers&gt;
143                         &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;
144                         &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;
145                         &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;
146                         &lt;provider name="Mono.Data.TdsClient" connection="Mono.Data.TdsClient.TdsConnection" adapter="Mono.Data.TdsClient.TdsDataAdapter" assembly="Mono.Data.TdsClient" /&gt;
147                         &lt;provider name="Mono.Data.MySql" connection="Mono.Data.MySql.MySqlConnection" adapter="Mono.Data.MySql.MySqlDataAdapter" assembly="Mono.Data.MySql" /&gt;
148                         &lt;provider name="Mono.Data.PostgreSqlClient" connection="Mono.Data.PostgreSqlClient.PgSqlConnection" adapter="Mono.Data.PostgreSqlClient.PgSqlDataAdapter" assembly="Mono.Data.PostgreSqlClient" /&gt;
149                         &lt;provider name="Mono.Data.SqliteClient" connection="Mono.Data.SqliteClient.SqliteConnection" adapter="Mono.Data.SqliteClient.SqliteDataAdapter" assembly="Mono.Data.SqliteClient" /&gt;
150                         &lt;provider name="Mono.Data.SybaseClient" connection="Mono.Data.SybaseClient.SybaseConnection" adapter="Mono.Data.SybaseClient.SybaseDataAdapter" assembly="Mono.Data.SybaseClient" /&gt;
151                 &lt;/providers&gt;
152         &lt;/mono.data&gt;
153 &lt;/configuration&gt;
155 </pre>