Can you use the OleDb classes for SQL Server?

  • Thread starter Thread starter Gustaf
  • Start date Start date
G

Gustaf

I'm in a situation where the customer has an Oracle DB, and my developing platform runs a SQL Server DB. Ideally, I'd like to write the code, and then simply change the connection string at deployment time. So my question is: can I use the OleDb classes rather than the native SqlClient classes, to connect to SQL Server, and then later to Oracle?

Gustaf
 
Howdy,

Yes you can. OleDb is the old style way of abstracting database providers.
The best way of doing abstraction is using stored procedures, as you can
delegate sql queries to database, otherwise you end up with different version
of queries for different RDBMSes. In addition, if you use .NET 2.0, it offers
another way od specialization for different RDBMS via providers and factories:
http://www.simple-talk.com/dotnet/.net-framework/ado.net-2.0-factory-classes/

Hoope this helps
 
I have yet to see this work without a lot of compromises. While OLE DB or
ODBC can obfuscate the backend, the code you write to execute and manage the
queries and the structures they return are very different for different
providers. Again, a layered data object approach seems to be a better way to
deal with this issue.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
Milosz said:
Yes you can. OleDb is the old style way of abstracting database providers.
The best way of doing abstraction is using stored procedures, as you can
delegate sql queries to database, otherwise you end up with different version
of queries for different RDBMSes.

I agree that it would be neat to just call a stored procedure for the INSERT method I need. But a stored procedure I write for SQL Server can't be moved directly to Oracle, can it? I know nothing about stored procedures in Oracle. So I think I need to put the INSERT statment in the C# code, or possibly in a config file.
In addition, if you use .NET 2.0, it offers
another way od specialization for different RDBMS via providers and factories:
http://www.simple-talk.com/dotnet/.net-framework/ado.net-2.0-factory-classes/

That's a good article. I was able to make use of the System.Data.SqlClient provider, but the OleDb provider did not work. When opening the connection, I get the useless error (type OleDbException):

No error message available, result code: DB_E_ERRORSOCCURRED(0x80040E21).

The app.config file currently looks like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="provider" value="System.Data.OleDb"/>
<add key="connectionString" value="Provider=SQLOLEDB;Data Source=.;Initial
Catalog=MyCompany;Integrated Security=true;User Instance=True"/>
</appSettings>
</configuration>

The solution would still be very useful, if I can just change "System.Data.SqlClient" to "System.Data.OracleClient" at deployment.

Gustaf
 
No. Oracle uses different syntax, data flow and other methodologies in their
SPs. They are not compatible with SQL Server. I would write a data layer
that uses the OracleClient .NET data provider to access Oracle.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
 
Howdy,

I didn't mean using the same stored procedures for different RDBMSes. I
meant you can use OleDb to call SPs (which is pretty much the same ADO.NET
code), but you would have to implement them differently for each RDBMS. In
addition, real life applications are much more advanced than simple SELECT,
INSERT, UPDATE statements, and that's why i mentioned you would end up with
different versions of queries for different RDBMS. I definitely agree with
William it's better to use abstraction in the code such as providers and i
mention about in my previous post.

Best regards
 
While both can be called in pretty much the same way, the way resultsets and
returned parameters and multiple rowsets are handled is very different.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
 
William said:
No. Oracle uses different syntax, data flow and other methodologies in their
SPs. They are not compatible with SQL Server. I would write a data layer
that uses the OracleClient .NET data provider to access Oracle.

So, to summarize, if I want to handle both SQL Server (which I need for development) and Oracle (for deployment), I shouldn't use stored procedures. Instead, I can write a DAL for SQL Server during development, and then use that as a template for an Oracle DAL?

I'm not so familiar with the topic, so I hope someone can point me to an example on how to design a DAL. What I imagine is a class with a method that takes my business object as input, and return an error code in case something goes wrong with the connection or the SQL command.

Gustaf
 
There are whole books on the subject and no, it's not easy to do. While I
mention the approach in my books, I don't recommend it--especially for the
inexperienced developer. Writing a data-centric application is more than
just setting up queries and returning results. It also involves managing
schemas, rights, uploads, downloads, procedures, backup, restore, exception
handling and a lot more. The exception handling alone is a monumental task
if you aren't developing against the actual database. While a data access
layer can be done, it has to be written with a clear and complete
understanding of both systems and how they interact with the low-level and
high-level interfaces.

If I were you, I would develop against the engine you expect to use during
production.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
INETA Speaker
www.betav.com/blog/billva
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
Visit www.hitchhikerguides.net to get more information on my latest book:
Hitchhiker's Guide to Visual Studio and SQL Server (7th Edition)
and Hitchhiker's Guide to SQL Server 2005 Compact Edition (EBook)
-----------------------------------------------------------------------------------------------------------------------
 
Back
Top