Getting Column lengths

Discussion of open issues, suggestions and bugs regarding ADO.NET provider for PostgreSQL
Post Reply
flxkid

Getting Column lengths

Post by flxkid » Wed 11 May 2005 18:03

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?

Yuri
Posts: 140
Joined: Mon 08 Nov 2004 12:07

Getting Column lengths

Post by Yuri » Thu 12 May 2005 09:36

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

Post Reply