We help IT Professionals succeed at work.
Get Started

How to fix warning CA2100 for OLEDB connection in C#

346 Views
Last Modified: 2019-12-28
Hi,

I need an expert view for the below code;

     public static string ExecuteScalarSingleValue(string dbPath, string tableName, string searchTerm, string searchColumnName,  string resultColumnName)
        {
            string connectionString = null;
            string sql = null;


            connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Data Source="+ dbPath;

            
            sql = "Select ["+ resultColumnName + "] from ["+ tableName + "] Where [" + searchColumnName + "] = '" + searchTerm + "'";

            System.Data.OleDb.OleDbConnection cnn = new System.Data.OleDb.OleDbConnection(connectionString);
            try
            {
                cnn.Open();
                System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand(sql, cnn);
                // Get the value from the database
                string result = (string)cmd.ExecuteScalar();
                cmd.Dispose();
                cnn.Close();
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }

        }

Open in new window


It gives a warning like on attached picture.AccessWarningCA2100.png
It directs me to this page: https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2100?view=vs-2019 

But i couldn't be able to fix that warning.

Any help would be grateful.
Comment
Watch Question
CERTIFIED EXPERT
Most Valuable Expert 2020
Most Valuable Expert 2018
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE