2010-12-15 85 views
0

我有这样功能没有返回第二列

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

这个功能应该与他们列数返回表名称的功能,但现在它只是返回的表名和列数不返回。

你们能帮我....

回答

0

您需要在您的数据读取器也得到了第二列(其值)!

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

这不会自动发生 - 你需要明确,并明确抓住从阅读器中的数据和存储它以某种方式(这样你就可以返回它)。