?
Solved

Dynamic Connection Strings in Microsoft Enterprise Library 3.1

Posted on 2007-11-25
4
Medium Priority
?
1,323 Views
Last Modified: 2013-11-26
I'm a complete beginner when it comes to .NET coding. I've been tasked to write a C# database class for the basic connect, dataset, datareader etc functions. It's going to replace existing VB code, so needs to be available to both WIndows apps and Excel VBA.

I've come across the Ent Lib and while it provides most of the functionaility, I like may others can't find a way to use the Database Factory with a dynamic connection string. My boss likes the functionality it provides, but wants to see another layer of encapsulation on top - i.e. a simple connect(connString) to connect to a db.

Is it worth looking at Ent Lib for this purpose or am I better off starting from scratch? Can anyone provide a wrapper that encapsulates the functionality I need?
0
Comment
Question by:da_h
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:MogalManic
ID: 20349340
You could write a simple method that parses the connection string for the provider attribute and then return the proper connection object.  Maybe something like this:
static public IDbConnection CreateConnection(string ConnectionString) 
{
 if (ConnectionString==null || ConnectionString.Length==0)
   throw new Exception("Unknown or undefined datasource name");
 
 string[] ConnectionAttributes=ConnectionString.Split(';'); 
 string NewConnectionString="";
 foreach (string s in ConnectionAttributes) {
   string[] AttributeParts=s.Split('=');
   string attributeName=AttributeParts[0].ToLower().Trim();
   if (attributeName=="provider") 
   {
     string provider=AttributeParts[1].ToLower();
     if (provider == "system.data.sqlclient")
     {
        return new SqlConnection(connectionString);
     } 
     else if (provider == "system.data.oledb")
     {
        return new OleDbConnection(connectionString);
     }
     else if (provider == "system.data.oracleclient")
     {
        return new OracleConnection(connectionString);
     }
   }
   throw new Exception("Missing 'provider' attribute in connection string");
}

Open in new window

0
 

Author Comment

by:da_h
ID: 20393467
Thanks Mogal....sorry for the slow reply, I've been interstate.

Although this is along the lines of what I'm looking for. Ideally what I'd like to know is if there is a somewhat complete database class / wrapper that you tend to use as the basis of a database dependent project, i.e something that handles opening, closing, stored procs, querying etc that can then be customised to suit the projects needs.

I'm not looking for someone to write the complete class for me but would assume that a relatively complete (and hopefully well coded) example exists out there somewhere.





0
 
LVL 21

Accepted Solution

by:
MogalManic earned 1500 total points
ID: 20394937
Sorry,

Most of the code I write is proprietary or uses a database mapping tool called IBATIS (http://ibatis.apache.org/).

I did a quick search of Code project and found the following:
  - http://www.codeproject.com/cs/database/Multi-Tier.asp - looks like a wrapper around the DataTable class
  - http://www.codeproject.com/cs/database/CustomCommandBuilder.asp - Typed dataset wrapper
  - http://www.codeproject.com/cs/database/MpmCodeproject.asp - Wrapper for open source Mimer (http://developer.mimer.com/mpm/)
  - http://www.codeproject.com/cs/database/persistedobject.asp - Generic persistance wrapper
  - http://www.codeproject.com/vb/net/data_access_layer.asp - THIN ADO.net wrapper (written in vb.net)
  - http://www.codeproject.com/cs/database/SimpleQuery.asp - Simple query methods (needs support for parameters added)

You can take samples from the above and build your framework.
0
 

Author Closing Comment

by:da_h
ID: 31410926
Thanks

I've since found a couple of good examples to get me going. Some of your links have helped fill in the gaps.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question