2 // Mono.Data.ProviderFactory
\r
5 // Brian Ritchie (brianlritchie@hotmail.com)
\r
8 // Copyright (C) Brian Ritchie, 2002
\r
13 // Permission is hereby granted, free of charge, to any person obtaining
\r
14 // a copy of this software and associated documentation files (the
\r
15 // "Software"), to deal in the Software without restriction, including
\r
16 // without limitation the rights to use, copy, modify, merge, publish,
\r
17 // distribute, sublicense, and/or sell copies of the Software, and to
\r
18 // permit persons to whom the Software is furnished to do so, subject to
\r
19 // the following conditions:
\r
21 // The above copyright notice and this permission notice shall be
\r
22 // included in all copies or substantial portions of the Software.
\r
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
34 using System.Reflection;
\r
35 using System.Runtime.Remoting;
\r
36 using System.Configuration;
\r
38 using System.Collections.Specialized;
\r
42 public class ProviderFactory
\r
44 private static ProviderCollection providers;
\r
46 static ProviderFactory()
\r
48 providers=(ProviderCollection) ConfigurationSettings.GetConfig("mono.data/providers");
\r
49 if (providers==null)
\r
50 providers=new ProviderCollection();
\r
53 static public ProviderCollection Providers
\r
61 static public IDbConnection CreateConnectionFromConfig(string Setting)
\r
63 return CreateConnection(ConfigurationSettings.AppSettings[Setting]);
\r
66 static public IDbConnection CreateConnection(string ConnectionString)
\r
68 string[] ConnectionAttributes=ConnectionString.Split(new Char[1] { ';' });
\r
69 string ProviderName=null;
\r
70 string NewConnectionString="";
\r
71 foreach (string s in ConnectionAttributes)
\r
73 string[] AttributeParts=s.Split(new Char[1] { '=' });
\r
74 if (AttributeParts[0].ToLower().Trim()=="factory")
\r
75 ProviderName=AttributeParts[1].Trim();
\r
77 NewConnectionString+=";"+s;
\r
79 NewConnectionString=NewConnectionString.Remove(0,1);
\r
80 return CreateConnection(ProviderName, NewConnectionString);
\r
83 static public IDbConnection CreateConnection(string ProviderName, string ConnectionString)
\r
85 Provider provider=providers[ProviderName];
\r
86 IDbConnection conn=provider.CreateConnection();
\r
87 conn.ConnectionString=ConnectionString;
\r
91 static public IDbCommand CreateStoredProc(IDbConnection Conn, string CommandName)
\r
93 IDbCommand cmd=Conn.CreateCommand();
\r
94 cmd.CommandText=CommandName;
\r
95 cmd.CommandType=CommandType.StoredProcedure;
\r
99 static public IDbDataAdapter CreateDataAdapter(IDbCommand SelectCommand)
\r
101 Provider provider=providers.FindByCommandType(SelectCommand.GetType());
\r
102 IDbDataAdapter adapter=provider.CreateDataAdapter();
\r
103 adapter.SelectCommand=SelectCommand;
\r
107 static public IDbDataAdapter CreateDataAdapter(string ProviderName)
\r
109 Provider provider=providers[ProviderName];
\r
110 IDbDataAdapter adapter=provider.CreateDataAdapter();
\r
114 static public IDbDataAdapter CreateDataAdapter(IDbConnection Conn, string SelectCommand)
\r
116 IDbCommand cmd=Conn.CreateCommand();
\r
117 cmd.CommandText=SelectCommand;
\r
118 return CreateDataAdapter(cmd);
\r
121 static public IDbCommand CreateCommand(string ProviderName)
\r
123 Provider provider=providers[ProviderName];
\r
124 return provider.CreateCommand();
\r
127 static public IDbCommand CreateCommand(IDbConnection Conn)
\r
129 return Conn.CreateCommand();
\r