views:

433

answers:

0

This Get() - method is intended to collect the column-names and their data-types from an SQL Server database-table.

public partial class Column
{
    //Properties
    ....................

    public static List<Column> Get(string databaseName, string tableName)
    {
     List<Column> columns = null;

     try
     {
      ORMapper orm = new ORMapper();

      bool success = orm.BeginTransaction();

      if (success)
      {
       orm.ExecuteNonQuery("use [" + databaseName + "]");

       IDataReader reader = orm.ExecuteReader(@"select * from [" + tableName + "]");

       DataTable dataTable = reader.GetSchemaTable();

       Column col = null;
       foreach (DataRow dataRow in dataTable.Rows)
       {
        col = new Column();
        col.ColumnName = dataRow["ColumnName"].ToString();
        col.DataType = dataRow["DataType"].ToString();
        col.DatabaseName = dataRow["BaseCatalogName"].ToString();
        col.TableName = dataRow["BaseTableName"].ToString();

        if (columns == null)
        {
         columns = new List<Column>();
        }

        columns.Add(col);
       }

       reader.Close();

       orm.EndTransaction();
      }
     }
     catch (Exception ex)
     {
      columns = null;
      throw ex;
     }

     return columns;
    }
}

Why is this code snippet throwing the following exception?

Procedure 'SYSREMOTE_CATALOGS' expects parameter 'ServerName', which was not supplied.

Please note that, this exception is thrown occasionally. And if Column.Get() is called repeatedly in a loop.

How to solve this problem?