函数未返回第二列

时间:2010-12-15 06:13:33

标签: winforms

我有这样的功能

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

这个函数应该返回包含它们所拥有的列数的表名,但现在它只返回表名,并且不返回列数。

你们可以帮助我......

1 个答案:

答案 0 :(得分:0)

您还需要在数据读取器中获取第二列(其值)!

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

这不会自动发生 - 您需要专门并明确地从阅读器中获取数据并以某种方式存储它(以便您可以将其返回)。