Page 1 of 1

Getting Column lengths

Posted: Wed 11 May 2005 18:03
by flxkid
As part of another process I need to get the lengths of the columns returned in a query. My test code for this looks like:

Code: Select all

DataSet ds = new DataSet();
PgSqlDataAdapter da = new PgSqlDataAdapter("select * from insured where company_name ilike '%out%'", conn);
da.SelectCommand = new PgSqlCommand("select * from insured where company_name ilike '%out%'", conn);
da.Fill(ds);
DataTable insured_dt = ds.Tables["Table"];
insured_dt.TableName = "insured";

for (int i=0; i < (results.TableStyles["insured"].GridColumnStyles.Count-1); i++)
{
	MessageBox.Show(insured_dt.Columns[i].MaxLength.ToString());
}
The messagebox always shows -1 though. Why isn't the column max length set?

Getting Column lengths

Posted: Thu 12 May 2005 09:36
by Yuri
You receive DataColumn.MaxLength property always set to -1 because it is used only for typed datasets.
To get column size information you may use PgSqlDataReader.GetSchemaTable() method.
See below:

Code: Select all

      PgSqlCommand command = connection.CreateCommand();
      command.CommandText = "select * from dept";
      PgSqlDataReader reader = command.ExecuteReader(CommandBehavior.KeyInfo);
      DataTable schemaTable= reader.GetSchemaTable();
      for (int i = 0; i < schemaTable.Rows.Count - 1; i++) { 
        MessageBox.Show(schemaTable.Rows[i]["ColumnSize"].ToString()); 
      }