troubleshooting Question

How to fix warning CA2100 for OLEDB connection in C#

Avatar of Skale
Skale asked on
DatabasesMicrosoft AccessC#SQL
6 Comments1 Solution353 ViewsLast Modified:
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;
            }

        }

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.
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros