2010-12-15 9 views
0

J'ai une fonction comme celui-ciFonction ne retourne pas la deuxième colonne

public static string GetNumberOfColumns(string ConnectionString) 
{ 
    StringBuilder sb = new StringBuilder(); 

    try 
    { 
     //Instance of SQL connection is created. 
     using(var sConnection = new SqlConnection(ConnectionString)) 

     //Instance of SQL command is created. 
     using(var sCommand = sConnection.CreateCommand()) 
     { 
      //Opens the connection. 
      sConnection.Open(); 

      //Query to get the list of tables and the number of columns they have. 
      sCommand.CommandText = @"SELECT 
                t.table_name 
               AS 
                TABLES, 

                COUNT(*) 
               FROM 
                INFORMATION_SCHEMA.TABLES t  
               JOIN 
                INFORMATION_SCHEMA.COLUMNS c 
               ON 
                c.table_name = t.table_name 
               WHERE 
                t.table_name <> 'dtProperties' 
              GROUP BY 
                t.table_name "; 

      using(var reader = sCommand.ExecuteReader()) 
      { 
       while(reader.Read()) 
       { 
       sb.AppendLine(reader.GetString(0)); 
       } 
      } 
     } 
    } 
    catch(Exception ex) 
    { 
     //All the exceptions are handled and written in the EventLog. 
     EventLog log = new EventLog("Application"); 
     log.Source = "MFDBAnalyser"; 
     log.WriteEntry(ex.Message); 
    } 

    return sb.ToString(); 
} 

Cette fonction doit renvoyer les noms de table avec le nombre de colonnes qu'ils ont, mais maintenant il revient uniquement les noms de table et le nombre de colonnes n'est pas retourné.

Pouvez-vous les gars me aider ....

Répondre

0

Vous devez également obtenir la deuxième colonne (sa valeur) dans votre lecteur de données !!

using(var reader = sCommand.ExecuteReader()) 
{ 
    while(reader.Read()) 
    { 
     int count = reader.GetInt(1); 
     sb.AppendLine(reader.GetString(0)); 
    } 
} 

Cela ne se produit pas automatiquement - vous devez saisir spécifiquement et explicitement les données du lecteur et le stocker en quelque sorte (afin de pouvoir y retourner).

Questions connexes