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 7417025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:37:01+00:00 2026-05-29T07:37:01+00:00

I have an application that will, among other things, store various data into a

  • 0

I have an application that will, among other things, store various data into a database.
The database might be ORACLE or SQL Server. The SQL is created dynamically based on configuration and values picked up during execution.

By using DbProviderFactory my db methods are able to work with either ORACLE or SQL Server without writing custom code for any of the databases, except from one thing; parameters/bind variables. For ORACLE I need to use ":ParameterName" whereas for SQL Server I need to use "@ParameterName". Is there any way to make this generic?

Sample code:

public class DbOperations
{
    private DbProviderFactory m_factory;
    private DbConnection m_CN;

    ...

    private void InsertToDb(ValueType[] values, ColumnType[] columns)
    {     
        DbCommand Cmd = m_factory.CreateCommand();
        Cmd.Connection = m_CN;

        StringBuilder sql = new StringBuilder();
        sql.Append("INSERT INTO ");
        sql.Append(DestinationTable);
        sql.Append(" (");

        for (int i = 0; i < columns.Length; i++)
        {
            sql.Append(columns[i].ColumnName);
            if (i < columns.Length - 1) 
            sql.Append(", ");
        }
        sql.Append(") VALUES (");

        for (int i = 0; i < values.Length; i++)
        {        
            //sql.Append(String.Format(":{0}", columns[i].ColumnName));  //ORACLE
            sql.Append(String.Format("@{0}", columns[i].ColumnName)); // SQL Server
        }       

        DbParameter param = m_factory.CreateParameter();
        param.Direction = ParameterDirection.Input;
        param.ParameterName = columns[i].ColumnName;
        param.Value = values[i];
        Cmd.Parameters.Add(param);

        if (i < columns.Length - 1)           
            sql.Append(", ");
      }
      sql.Append(")");
      Cmd.CommandText = sql.ToString();
      Cmd.ExecuteNonQuery();
}
  • 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. Editorial Team
    Editorial Team
    2026-05-29T07:37:02+00:00Added an answer on May 29, 2026 at 7:37 am

    I accepted an answer for this question long ago, but for some reason that answer is no longer here… So I guess I need to answer my own question.

    What I did was to create a parambuilder class:

    class ParamBuilder
    {
        private DbProviderFactory m_factory;
        private DbCommandBuilder m_builder;
        private string m_parameterMarkerFormat;
        public ParamBuilder(DbProviderFactory factory) : this(factory, null)
        {
        }
    
        public ParamBuilder(DbProviderFactory factory, DbConnection source)
        {
            m_factory = factory;
            m_builder = m_factory.CreateCommandBuilder();
            if (source != null)
            {
                using (DataTable tbl =
                    source.GetSchema(DbMetaDataCollectionNames.DataSourceInformation))
                {
                    m_parameterMarkerFormat =  
                        tbl.Rows[0][DbMetaDataColumnNames.ParameterMarkerFormat] as string;
                }
            }
            if (String.IsNullOrEmpty(m_parameterMarkerFormat))
                m_parameterMarkerFormat = "{0}";
        }
    
        public DbParameter CreateParameter(string parameterName, 
            out string parameterMarker)
        {
            DbParameter param = m_factory.CreateParameter();
            param.ParameterName =  
                (string)typeof(DbCommandBuilder).InvokeMember("GetParameterName",
                    System.Reflection.BindingFlags.Instance |
                    System.Reflection.BindingFlags.InvokeMethod |
                    System.Reflection.BindingFlags.NonPublic, null, m_builder, 
                    new object[] { parameterName });
    
            parameterMarker = 
                String.Format(System.Globalization.CultureInfo.InvariantCulture, 
                m_parameterMarkerFormat, param.ParameterName);
    
            return param;
        }
    
    }
    

    I create a member variable of the ParamBuilder type:

    private readonly ParamBuilder m_ParamBuilder;
    

    Then in the method where I use parameters, I use it as follows:

    ...
    string paramMarker;
    DbParameter param = m_ParamBuilder.CreateParameter(destination[i].ColumnName, 
        out paramMarker);
    sql.Append(paramMarker);
    
    param.Direction = ParameterDirection.Input;
    param.Value = source[i];
    Cmd.Parameters.Add(param);
    ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an application that will have a SQL Server backend that will store
I have an application that interacts with another application using SendMessage (among other things).
I have a PHP web application (running on Apache/Linux) that, among other things, allows
The reason I ask is that I have an application that (among other things)
We have an application that will be collecting data and storing it in local
I plan to build a WinForms application that will contain among other controls a
I am creating application in Winform which among other things will also need to
I'm working on an application which among other things downloads items that belong to
I need to write an application that (among the other things) renders 2D animated
I have a web application which among other things contains a table of items

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.