Getting Column lengths

Getting Column lengths

Postby 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);
DataTable insured_dt = ds.Tables["Table"];
insured_dt.TableName = "insured";

for (int i=0; i < (results.TableStyles["insured"].GridColumnStyles.Count-1); i++)

The messagebox always shows -1 though. Why isn't the column max length set?

Getting Column lengths

Postby 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++) {
Posts: 140
Joined: Mon 08 Nov 2004 12:07

Return to dotConnect for PostgreSQL