Advertisement

03.04.2008 at 07:51AM PST, ID: 23212946
[x]
Attachment Details

Switching between database providers in web.config / Data Access Layer

Asked by gjok in .NET, Microsoft Visual C#.Net

Tags: c#, asp.net

Hi.
I've been looking at a basic example of a web application that allows you to specify between MS Access or SQL Server in the web.config. (see snippet part 1 below)

Now, as both these providers use ifferent sql syntax, I understand that I will need to parse my sql statements accordingly as shown in snippet part.3.

However, how do I handle things like "getdate()" which is not supported by Access? Am I to have separate code blocks for handling Access and sql server? If so isnt the following line kind of pointless?:

param.ParameterName = DalHelpers.ReturnCommandParamName("startDate")

-or- should I just NOT use "getdate()" and build the date manually?

Also, am I correct in assuming that the code in snippet part.2 will handle the dates (sql uses a quote and access uses a '#' symbol)
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
Part.1 - Web.config:
 
  <appSettings>
    <add key="DefaultConnectionString" value="SqlServerConString" />
  </appSettings>
 
  <connectionStrings>
    <add name="SqlServerConString" connectionString="server=(local)\SqlExpress etc, etc, " providerName="System.Data.SqlClient" />
    <add name="AccessConString" connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Inetpub\wwwrootI etc, etc ;" providerName="System.Data.OleDb" />
  </connectionStrings>
 
 
 
Part.2 - Code
      param = myCommand.CreateParameter()
      param.ParameterName = DalHelpers.ReturnCommandParamName("startDate")
      param.DbType = DbType.DateTime
      param.Value = startDate
      myCommand.Parameters.Add(param)
 
 
 
Part.3 - Class:
 
  Public Shared Function ReturnCommandParamName( _
      ByVal paramName As String) As String
    Dim returnValue As String = String.Empty
    Select Case AppConfiguration.ConnectionStringSettings.ProviderName.ToLower()
      Case "system.data.sqlclient"
        returnValue = "@" & paramName
      Case "system.data.oledb"
        returnValue = "?"
      Case Else
        Throw New NotSupportedException("The provider " & _
            AppConfiguration.ConnectionStringSettings.ProviderName & " is not supported")
    End Select
    Return returnValue
  End Function
 
 
Loading Advertisement...
 
[+][-]03.04.2008 at 08:44AM PST, ID: 21042570

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.04.2008 at 08:52AM PST, ID: 21042648

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.04.2008 at 11:59AM PST, ID: 21044445

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.05.2008 at 12:57AM PST, ID: 21048701

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.05.2008 at 01:11AM PST, ID: 21048763

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.05.2008 at 03:51AM PST, ID: 21049513

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.05.2008 at 06:51AM PST, ID: 21050723

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.06.2008 at 01:20PM PST, ID: 21064893

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: .NET, Microsoft Visual C#.Net
Tags: c#, asp.net
Sign Up Now!
Solution Provided By: nsanga
Participating Experts: 3
Solution Grade: A
 
 
[+][-]03.06.2008 at 04:40PM PST, ID: 21066234

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.07.2008 at 05:57AM PST, ID: 21069937

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.07.2008 at 06:02AM PST, ID: 21069984

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03.07.2008 at 09:49AM PST, ID: 21072597

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.08.2008 at 06:33AM PST, ID: 21077066

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 7-day free trial to view this Assisted Solution or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628