Link to home
Start Free TrialLog in
Avatar of Skale
Skale

asked on

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

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());
            }
        }

Open in new window


You can find sample database on attachment also.

User generated imageASampleDatabase.accdb
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

SELECT [Asset Items].[Asset No]
FROM [Asset Items]
WHERE ((([Asset Items].[Serial No])="DF556-33446"));

Open in new window

ASampleDatabase-DWJ.accdb
ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Skale
Skale

ASKER

Thank you for contributions :) Now i got the idea. Fernando it's worked! and David it's also useful to get reverse value for me :)