Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 64567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:48:36+00:00 2026-05-10T18:48:36+00:00

I am developing an application which will be connected to Access database at the

  • 0

I am developing an application which will be connected to Access database at the beginning and the plan is to switch to MS SQL or SQL Express in the near future. The datatables structures are same for both types of databases and I am trying to avoid duplicating the code and trying to find the way to minimize the code.

For example I wrote the following function for retrieving data from Access database:

public static DataTable GetActiveCalls()     {         string select = 'SELECT call_id, call_time, msisdn, status FROM call WHERE status = 0 OR status = 1 ORDER by call_id ASC';         OleDbCommand cmd = new OleDbCommand(select, conn);         DataTable dt = new DataTable('Active Calls');         OleDbDataAdapter DA = new OleDbDataAdapter(cmd);         try         {             conn.Open();             DA.Fill(dt);         }         catch (Exception ex)         {             string sDummy = ex.ToString();         }         finally         {             conn.Close();         }         return dt;     } 

and the following code is for SQL Express database:

public static DataTable GetActiveCalls()     {         string select = 'SELECT call_id, call_time, msisdn, status FROM call WHERE status = 0 OR status = 1 ORDER by call_id ASC';         SqlCommand cmd = new SqlCommand(select, conn);         DataTable dt = new DataTable('Active Calls');         SqlDataAdapter DA = new SqlDataAdapter(cmd);         try         {             conn.Open();             DA.Fill(dt);         }         catch (Exception ex)         {             string sDummy = ex.ToString();         }         finally         {             conn.Close();         }         return dt;     } 

These two methods are almost the same. The only differences are SqlCommand/OleDbCommand and SqlDataAdapter/OleDbDataAdapter. There are also some methods which take arguments for example:

public static void AddMessage(string callID, string content)     {         string select =             'INSERT INTO message(key, direction, content, read, write_time) VALUES (@callId, 0, @content, 0, @insertTime)';         OleDbCommand cmd = new OleDbCommand(select, conn);         cmd.Parameters.AddWithValue('callId', callID.ToString());         cmd.Parameters.AddWithValue('content', content);         cmd.Parameters.AddWithValue('insertTime', DateTime.Now.ToString());         try         {             conn.Open();             cmd.ExecuteScalar();         }         catch (Exception ex)         {             string sDummy = ex.ToString();         }         finally         {             conn.Close();         }     } 

In this case SQL query string is also the same for both databases but there is the difference between the type of cmd (SqlCommand/OleDbCommand).

I would really appreciate if anyone could give any suggestion about how to avoid duplicating the code and optimize the given problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:48:37+00:00Added an answer on May 10, 2026 at 6:48 pm

    You can use the database independent interfaces IDbDataAdapter and IDbCommand, and then create the concrete instances using a factory. Here is an example.

    However I would reccommend using an ORM solution like NHibernate if your application is not very simple, because there are little differences in the SQL language between Access and SQL Server that can make your data access code more complex.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 120k
  • Answers 120k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer To be honest, if you don't know the type at… May 12, 2026 at 12:12 am
  • Editorial Team
    Editorial Team added an answer The question is rather vague on the requirements, so it's… May 12, 2026 at 12:12 am
  • Editorial Team
    Editorial Team added an answer This should work, #!/usr/bin/perl # File: convert.pl no utf8; #… May 12, 2026 at 12:12 am

Related Questions

I am developing a rich client application that will use the Entity Framework (with
I am developing an application which includes a WCF service and its ASP.NET MVC
I am developing an application where I have a requirement that I have to
I am new to iPhone development, so I'm sorry if this is a stupid

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.