[Mono-list] DbDataAdapter in ADO.NET Provider Factory
Daniel Morgan
danmorg@sc.rr.com
Mon, 11 Nov 2002 14:14:52 -0500
Brian,
Can you add the ability to dynamically create a DbDataAdapter for a given
connection class generically?
I would l like to use the adapter in provider generic code. I've also
provided sample code that could create the DbDataAdapter for an ADO.NET data
provider.
What do you think?
// data provider generic code
IDbConnection conn =
ProviderFactory.CreateConnectionFromConfig("TdsPubsConnStr");
CreateXmlFileFromSqlQuery("select * from employee", "employee.xml",
conn, "Mono.Data.TdsClient");
public void CreateXmlFileFromSqlQuery (string sql, string filename,
IDbConnection conn, string providerAssembly) {
IDbCommand cmd = conn.CreateCommand();
// set command properties
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Connection = conn;
DataSet dataSet = new DataSet ();
DataAdapter adapter = CreateNewDataAdapter (cmd, conn);
adapter.Fill (dataSet, "Table1");
dataSet.WriteXml (filename);
}
// sample code to create new data adapter generically
public DataAdapter CreateNewDataAdapter (string provider,
string providerAssembly,
IDbConnection connection, IDbCommand command) {
DataAdapter adapter = null;
switch(provider) {
case "ODBC":
adapter = (DataAdapter) new OdbcDataAdapter ();
break;
case "OLEDB":
adapter = (DataAdapter) new OleDataAdapter ();
break;
case "SQLCLIENT":
adapter = (DataAdapter) new SqlDataAdapter ();
break;
case "LOADEXTPROVIDER":
adapter = CreateExternalDataAdapter ();
if (adapter == null)
return;
break;
default:
Console.WriteLine("Error: Data Adapter not found in provider.");
return;
}
IDbDataAdapter dbAdapter = (IDbDataAdapter) adapter;
dbAdapter.SelectCommand = command;
return adapter;
}
// sample code for loading the concrete subclass of DbDataAdapter
// Thanks to Gonzalo for helping me with this
public DataAdapter CreateExternalDataAdapter (string providerAssembly) {
Assembly ass = Assembly.Load (providerAssembly);
Type [] types = ass.GetTypes ();
foreach (Type t in types) {
if (t.IsSubClassOf ("System.Data.DbDataAdapter") == false)
continue;
}
if (t.IsSubClassOf ("System.Data.DbDataAdapter") == false)
return null; // adapter class not found
adapter = (DataAdapter) Activator.CreateInstance (t);
return adapter;
}
-----Original Message-----
From: mono-list-admin@ximian.com [mailto:mono-list-admin@ximian.com]On
Behalf Of Brian Ritchieo u
Sent: Saturday, November 09, 2002 11:59 AM
To: rodrigo@ximian.com
Cc: mono-list@ximian.com
Subject: Re: [Mono-list] ADO.NET Provider Factory
Rodrigo,
Thanks for the review. I guess great minds think alike...I actually already
had a CreateConnectionFromConfig. :)
Here's the syntax for it:
IDbConnection
conn=ProviderFactory.CreateConnectionFromConfig("PubsConnStr");
<appSettings>
<add key="PubsConnStr" value="factory=System.Data.SqlClient;
server=speedy;database=pubs;uid=sa;pwd=" />
</appSettings>
The factory attribute specifies which provider to use. I parse out the
factory attribute, crete the object, and then pass the rest of the
connection string into the provider. The providers are definited in the
app.config (or machine.config).
I'll get the code commited sometime today.
Brian
>From: Rodrigo Moya <rodrigo@ximian.com>
>To: Brian Ritchie <brianlritchie@hotmail.com>
>CC: Mono List <mono-list@ximian.com>
>Subject: Re: [Mono-list] ADO.NET Provider Factory
>Date: 09 Nov 2002 12:15:52 +0100
>
>On Mon, 2002-11-04 at 06:37, Brian Ritchie wrote:
> > I've started work on the set of objects known as... "ADO.NET
> > Multiplexor Provider" in... http://www.go-mono.com/ado-net.html
> >
> > I'd appreciate the ADO.NET team to review this (and attached files)
> > before I commit things to CVS. I'm planning to put them under
> > /mcs/class/Mono.Data. Please note: connection strings will in attached
> > files will need to be updated to match your configuration. I've done
> > some basic testing of the code under MS.NET & Mono on Windows.
> >
>it looks ok to me. I would also add one thing though, which is a way to
>activate a given provider/connection string without having to know the
>provider to be used. That is, something like the ODBC (or libgda) data
>sources, on which you store, under a unique name, all parameteres needed
>for the connection to be established. Something like:
>
>IDbConnection cnc = ProviderFactory.CreateConnectionFromConfig
>("my_data_source");
>
>and then have, in the config file, an entry called "my_data_source"
>which contains the info for the provider and connection string to be
>used to open this given connection.
>
>Apart from that, it looks ok to me, so please commit as soon as you can.
>
>and sorry for the delay in answering, but I've been really busy these
>last weeks.
>
>cheers
>--
>Rodrigo Moya <rodrigo@ximian.com>
_________________________________________________________________
Help STOP SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail
_______________________________________________
Mono-list maillist - Mono-list@ximian.com
http://lists.ximian.com/mailman/listinfo/mono-list