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:
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>
17 ** Overview of the ProviderFactory object model:
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>
42 ** C# source code samples for creating a connection:
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).
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
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 <appSettings>
68 string appSetting = "PubsConnStr";
69 conn = ProviderFactory.CreateConnectionFromConfig(appSetting);
73 <p>C# Sample for Creating a DataAdapter and filling a DataSet.
78 ProviderFactory.CreateConnectionFromConfig("PubsConnStr");
81 IDbCommand cmd=conn.CreateCommand();
82 cmd.Text="select * from author";
85 DataSet ds=new DataSet();
86 IDbDataAdapter adapter=ProviderFactory.CreateDataAdapter(cmd);
87 adapter.Fill(ds, "Table1");
90 <p>Creating a DataAdapter and filling a DataSet. <br>
91 The super lazy method for people like me.
95 ProviderFactory.CreateConnectionFromConfig("TdsPubsConnStr");
98 DataSet ds=DataTools.FillDataSet(conn, "select * from author");
102 <p>Here's some sample code on displaying a list <br>
103 of configured ADO.NET providers:
105 Console.WriteLine("Configured Providers:");
106 foreach (Provider p in ProviderFactory.Providers)
107 Console.WriteLine(p.Description);
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.
114 DataSet ds=DataTools.FillDataSet("PubsConnStr", "select * from authors");
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:
131 <?xml version="1.0" encoding="utf-8" ?>
132 <configuration>
133 <configSections>
134 <sectionGroup name="mono.data">
135 <section name="providers" type="Mono.Data.ProviderSectionHandler,Mono.Data" />
136 </sectionGroup>
137 </configSections>
139 <add key="PubsConnStr" value="factory=System.Data.SqlClient;server=speedy;database=pubs;uid=sa;pwd=" />
143 <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"/>
144 <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"/>
145 <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"/>
146 <provider name="Mono.Data.TdsClient" connection="Mono.Data.TdsClient.TdsConnection" adapter="Mono.Data.TdsClient.TdsDataAdapter" assembly="Mono.Data.TdsClient" />
147 <provider name="Mono.Data.MySql" connection="Mono.Data.MySql.MySqlConnection" adapter="Mono.Data.MySql.MySqlDataAdapter" assembly="Mono.Data.MySql" />
148 <provider name="Mono.Data.PostgreSqlClient" connection="Mono.Data.PostgreSqlClient.PgSqlConnection" adapter="Mono.Data.PostgreSqlClient.PgSqlDataAdapter" assembly="Mono.Data.PostgreSqlClient" />
149 <provider name="Mono.Data.SqliteClient" connection="Mono.Data.SqliteClient.SqliteConnection" adapter="Mono.Data.SqliteClient.SqliteDataAdapter" assembly="Mono.Data.SqliteClient" />
150 <provider name="Mono.Data.SybaseClient" connection="Mono.Data.SybaseClient.SybaseConnection" adapter="Mono.Data.SybaseClient.SybaseDataAdapter" assembly="Mono.Data.SybaseClient" />
153 </configuration>