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

  • Home
  • SEARCH
  • 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 181k
  • Answers 181k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes! With the new version 7.0 gdb, you can do… May 12, 2026 at 4:19 pm
  • Editorial Team
    Editorial Team added an answer Try: git show --pretty=format: <commitid> -- <file> Here's how it… May 12, 2026 at 4:19 pm
  • Editorial Team
    Editorial Team added an answer You can't -- arrays are not lvalues in C. You'll… May 12, 2026 at 4:19 pm

Related Questions

Currently following is the setup of my wifi connection. Laptop 1 ------> Wifi Router
I am developing an Erlang application which requires a LOT of DB writes. My
I am developing a rich client application that will use the Entity Framework (with
For years Windows XP (32 bit)was my development desktop (personal projects). I develop many

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.