troubleshooting Question

How to create function that returns a single value and single row from MS Access Database in C#

Avatar of Skale
Skale asked on
DatabasesMicrosoft AccessC#SQL
3 Comments1 Solution143 ViewsLast Modified:
Hello,

I would like to create a function and return a single value like an example;

public static string GetValueFromAccess(string uniqueID) {....}

As in example i'd like to get result "DF556-33446" when i set uniquID = 30075

I'm trying to edit this code to implement it but didn't succeeded.

        private void AccessReader_Click(object sender, RoutedEventArgs e)
        {
            string connetionString = null;
            OleDbConnection cnn;
            OleDbCommand cmd;
            string sql = null;

            connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Mode=Read;Data Source=E:\SampleData\ASampleDatabase.accdb";

            sql = "Select Count(*) from [Asset Items]";

            cnn = new OleDbConnection(connetionString);
            try
            {
                cnn.Open();
                MessageBox.Show("Connection Opened ");
                cmd = new OleDbCommand(sql, cnn);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
                cnn.Close();
                MessageBox.Show(" ExecuteNonQuery in OleDbConnection executed !! :");
            }
            catch (Exception ex)
            {
                MessageBox.Show("Can not open connection ! " + ex.ToString());
            }
        }

You can find sample database on attachment also.

AccessExample.pngASampleDatabase.accdb
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 3 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 3 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