Wednesday, March 28, 2012

How to find the Primary Keys in a table in Ms.Access database using c#?

This is my first blog. So Sorry for my English and choice of words.

Here is how I got the need to find a solution to how to find the primary keys in a column(s) in Ms. Access dynamically using a program:

I was developing a software for a mobile shop when there was a need to update his software to a complete new model. Then there was a need to update, rather copy, all his data to my new database and table structure. So I decided to dynamically create a Ms-Access database and copy a table into individual databases. So in order to dynamically create a table in newly created database, I had to find which column(s) or field(s) was/were the primary keys in the selected table so that I could use them to create a new table in the new database.

Here is how I did it. Hope it will be useful to you.
This part contains only how to get the primary key column(s) from a table.

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.OleDb;
using System.Data;

namespace PrimaryKey
{
    class Program
    {
        public string[] checkPrimaryKey(string tablename, OleDbConnection con)
        {
            con.Open();
            DataSet ds = new DataSet();
            OleDbDataAdapter da = new OleDbDataAdapter("select * from " + tablename, con);
            da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
            da.Fill(ds, tablename);
            DataColumn[] pks = ds.Tables[0].PrimaryKey;
            string[] primarykeys = new string[pks.Length];
            for (int i = 0; i < pks.Length; i++)
            {
                primarykeys[i] = pks[i].ColumnName;
            }
            con.Close();
            return primarykeys;
        }

        static void Main(string[] args)
        {           
            Program p=new Program();
            try
            {
                OleDbConnection conSrc = new OleDbConnection();
                //change database name
                string database = "manufacturers.mdb";
                conSrc.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + database + "; User Id=admin; Password=;";
                //change table name
                string table = "AccessoryManufacturer";
                string[] pkeys = p.checkPrimaryKey(table, conSrc);
                Console.WriteLine("Primary Keys in table : " + table + " are : ");
                for (int i = 0; i < pkeys.Length; i++)
                {
                    Console.WriteLine(pkeys[i]);
                }
                Console.Write("Press Enter to Exit ..... ");
                Console.ReadLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("Check database name and table name");
                Console.WriteLine(e.ToString());
                Console.Write("Press Enter to Exit ..... ");
                Console.ReadLine();
            }
        }
    }
}

You can also download the project from here.

If you have a better solution please do share.
Please Comment if it was a help for you. :)
Thanks.
Sandeep.R.Nair

No comments:

Post a Comment