DbProviderFactories.GetFactory(row) exception

G

Guest

Hi;

I have the following code:
DataTable table = DbProviderFactories.GetFactoryClasses();
foreach (DataRow row in DbProviderFactories.GetFactoryClasses().Rows)
DbProviderFactory providerFactory = DbProviderFactories.GetFactory(row);

The DbProviderFactories.GetFactory(row) throws an exception when the row has
the values: "SQL Server CE Data Provider", ".NET Framework Data Provider for
Microsoft SQL Server 2005 Mobile Edition", "Microsoft.SqlServerCe.Client",
"Microsoft.SqlServerCe.Client.SqlCeClientFactory,
Microsoft.SqlServerCe.Client, Version=9.0.242.0, Culture=neutral,
PublicKeyToken=89845dcd8080cc91"

The exception is:
System.Configuration.ConfigurationErrorsException was unhandled
Message="Failed to find or load the registered .Net Framework Data
Provider."
Source="System.Data"
BareMessage="Failed to find or load the registered .Net Framework Data
Provider."
Line=0
StackTrace:
at System.Data.Common.DbProviderFactories.GetFactory(DataRow
providerRow)
at DbTest.Program.Main(String[] args) in
c:\src\MyWordAddin\DbTest\Program.cs:line 17
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


What is going on?
 
K

Kevin Yu [MSFT]

Hi dave,

Based on my research, the Microsoft.SqlServerCe.Client assembly is
installed with SQL Server 2005 and VS.NET 2005. However, it was not
registered to the GAC. The assembly is at C:\Program Files\Microsoft Visual
Studio 8\Common7\IDE\Microsoft.SqlServerCe.Client.dll. You can add this to
GAC, so your code will run fine.

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Hello;

My concern is this call throwing an exception on user's systems. And I can't
tell them all to fix how the Sql Server CE client was installed. How can I
determine if I can call GetFactory() for a given row?

I don't want to have a try/catch because I think it is very poor programming
practice to have exceptions thrown &and caught) when the program is operating
normally.
 
K

Kevin Yu [MSFT]

You can check the content of the rows and skip Microsoft.SqlServerCe.Client.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
G

Guest

Hi;

I don't think that is a good solution. If they have Ce.Client installed,
then I don't want to skip it.

And what if they have another client that throws an exception?

I want to know how to avoid calling this methid fi, and only if, it will
throw an exception.
 
K

Kevin Yu [MSFT]

Hi dave,

The information is located in the machine.config file under
C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG folder. Here is an
example.

<system.data>
<DbProviderFactories>
<add name="Odbc Data Provider" invariant="System.Data.Odbc"
description=".Net Framework Data Provider for Odbc"
type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="OleDb Data Provider" invariant="System.Data.OleDb"
description=".Net Framework Data Provider for OleDb"
type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="OracleClient Data Provider"
invariant="System.Data.OracleClient" description=".Net Framework Data
Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory,
System.Data.OracleClient, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
<add name="SqlClient Data Provider" invariant="System.Data.SqlClient"
description=".Net Framework Data Provider for SqlServer"
type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<add name="SQL Server CE Data Provider"
invariant="Microsoft.SqlServerCe.Client" description=".NET Framework Data
Provider for Microsoft SQL Server 2005 Mobile Edition"
type="Microsoft.SqlServerCe.Client.SqlCeClientFactory,
Microsoft.SqlServerCe.Client, Version=9.0.242.0, Culture=neutral,
PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>

If other provider need themselves to be enumerated, they need to

1. add themselves to the GAC,
2. add a new node in the machine.config file.

Currently, they best way to workaround this is to use try/catch block. I
don't have other better ways.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top