开发者

Function is not returning the second column

开发者 https://www.devze.com 2023-01-31 04:32 出处:网络
I have a function like this public static string GetNumberOfColumns(string ConnectionString) { StringBuilder sb = new StringBuilder();

I have a function like this

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
                   开发者_如何学Go                               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();
}

This function should return the table names with the number of columns they have but now it is returning only the table names and the column count is not returned.

Can you guys help me out....


You need to also get the second column (its value) in your data reader!!

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

That doesn't happen automatically - you need to specifically and explicitly grab the data from the reader and store it somehow (so you can return it).

0

精彩评论

暂无评论...
验证码 换一张
取 消