DotNetNewsgroup.com  
web access to complete list of Microsoft.NET newsgroups
   home   |   control panel login   |   archive  |  
 
  carried group
academic
adonet
aspnet
aspnet.announcements
aspnet.buildingcontrols
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
assignment_manager
datatools
dotnet.distributed_apps
dotnet.general
dotnet.myservices
dotnet.nternationalization
dotnet.scripting
dotnet.security
dotnet.vjsharp
dotnet.vsa
dotnet.xml
dotnetfaqs
framework
framework.clr
framework.compactframework
framework.component_services
framework.controls
framework.databinding
framework.drawing
framework.enhancements
framework.interop
framework.odbcnet
framework.performance
framework.remoting
framework.sdk
framework.setup
framework.webservices
framework.windowsforms
framework.wmi
frwk.windowsforms.designtime
lang.csharp
lang.jscript
lang.vb
lang.vb.controls
lang.vb.data
lang.vb.upgrade
lang.vc
lang.vc.libraries
  
 
start date: Wed, 25 Jul 2007 02:30:12 -0700,    posted on: microsoft.public.dotnet.framework.adonet        back       

Thread Index
  1    unknown


How to get the primary key   
Hello there,

I want to write a function returning the primary key name of a certain
table.

The following function successfully returns the names of all fields of
a table:

       public string[] GetFieldNames(string tableName)
        {
            string[] fieldNames = null;
            IDbConnection conn = _factory.CreateConnection(true);
            try
            {
                string[] restrictions = { null, tableName, null };
                DataTable dataTable = (conn as
DbConnection).GetSchema("Columns", restrictions);
                if (dataTable != null)
                {
                    fieldNames = new string[dataTable.Rows.Count];
                    for (int i = 0; i < dataTable.Rows.Count; i++)
                        fieldNames[i] = dataTable.Rows[i]
["COLUMN_NAME"].ToString();
                }
                return fieldNames;
            }
            finally
            {
                conn.Close();
            }
        }

An interesting part in the upper function is, that the table name is
at the second position in the restrictions array instead on the third,
as I have seen several times in samples found in the internet. In my
case I get the field names with the table name at the second position
in the array.

Now I wrote a function to get the primary key of a table:

        public string GetPrimaryKeyName(string tableName)
        {
            IDbConnection conn = _factory.CreateConnection(true);
            try
            {
                string[] restrictions = { null, tableName,
tableName };
                DataTable dataTable = (conn as
DbConnection).GetSchema("PrimaryKeys", restrictions);
                if (dataTable != null)
                {
                    if (dataTable.Rows.Count > 0)
                        return dataTable.Rows[0]
["COLUMN_NAME"].ToString();
                }
                return null;
            }
            finally
            {
                conn.Close();
            }
        }

That function never returns me the primary key, regardless on which
position the table name is in the restrictions array. I heard that the
upper function does not work for connections based on Oracle. Is that
true?

Regards,
Norbert
Date:Wed, 25 Jul 2007 02:30:12 -0700   Author:  

Google
 
Web dotnetnewsgroup.com


COPYRIGHT ?2005, EUROFRONT WORLDWIDE LTD., ALL RIGHT RESERVE  |   Contact us