Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Column Length

Column Length

Scheduled Pinned Locked Moved Database
databasecsharpsql-serversysadminquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    Guinness4Strength
    wrote on last edited by
    #1

    I'm using ADO.NET and I'm attempting to get the column length for VARCHAR columns using the MaxLength property of the DataTable class, but it always returns -1, any secrets to finding this info from a returned MS SQL Server Query ?

    M 1 Reply Last reply
    0
    • G Guinness4Strength

      I'm using ADO.NET and I'm attempting to get the column length for VARCHAR columns using the MaxLength property of the DataTable class, but it always returns -1, any secrets to finding this info from a returned MS SQL Server Query ?

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      According to the docs "If the column has no maximum length, the value is –1 (default)." If you set MaxLength on a column do you still get -1 back? Mark

      G 1 Reply Last reply
      0
      • M Mark Salsbery

        According to the docs "If the column has no maximum length, the value is –1 (default)." If you set MaxLength on a column do you still get -1 back? Mark

        G Offline
        G Offline
        Guinness4Strength
        wrote on last edited by
        #3

        Yes, the length limit can be seen in the "Design Table" dialog of the SQL Server Enterprise Manager for the table in question. Its a VARCHAR(50) column, but after I query this tabkle the DataTable's DataColumn has a MaxLength value of -1 Here is the code i use to Query the Table if that helps...

        DataTable DT = new DataTable();
        if(m_DB is SqlConnection)
        {
        m_DA = new SqlDataAdapter(Query,(SqlConnection)m_DB);
        m_CB = new SqlCommandBuilder((SqlDataAdapter)m_DA);
        ((SqlCommandBuilder)m_CB).QuotePrefix="[";
        ((SqlCommandBuilder)m_CB).QuoteSuffix="]";
        ((SqlDataAdapter)m_DA).Fill(DT);
        }

        M 1 Reply Last reply
        0
        • G Guinness4Strength

          Yes, the length limit can be seen in the "Design Table" dialog of the SQL Server Enterprise Manager for the table in question. Its a VARCHAR(50) column, but after I query this tabkle the DataTable's DataColumn has a MaxLength value of -1 Here is the code i use to Query the Table if that helps...

          DataTable DT = new DataTable();
          if(m_DB is SqlConnection)
          {
          m_DA = new SqlDataAdapter(Query,(SqlConnection)m_DB);
          m_CB = new SqlCommandBuilder((SqlDataAdapter)m_DA);
          ((SqlCommandBuilder)m_CB).QuotePrefix="[";
          ((SqlCommandBuilder)m_CB).QuoteSuffix="]";
          ((SqlDataAdapter)m_DA).Fill(DT);
          }

          M Offline
          M Offline
          Mark Salsbery
          wrote on last edited by
          #4

          Ok, sorry. I use ODBC to get schema info in my database code, specifically SQLTables() and SQLColumns(). Take a look at GetSchemaTable (in DataReader). I'm pretty sure that retreives the same info as the ODBC functions. I think it's tough to use a specific SQL query because the way schema is stored varies by server, version, etc. Mark

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups