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. General Programming
  3. C#
  4. DataColumn type

DataColumn type

Scheduled Pinned Locked Moved C#
databasecsharpsql-serversysadminquestion
3 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.
  • D Offline
    D Offline
    denizmercan
    wrote on last edited by
    #1

    I have a Dataset that has returned data from SQL Server. I would like to read in what the SQL Server type is for each Column and what the size is. For instance is the column is a nvarchar(20) I would like to be able to find out that the column has a size of 20. Is this possible? I can't seem to figure this out even using the DataColumn. DataColumn.MaxLength is returning -1. I tried also SQL Command: SELECT COL_LENGTH('MyTable','MyColumnName') AS 'MyColumnLength' it's running fine in SQL Query analyzer but it's returning 0 in C# Any idea? thanks

    T 1 Reply Last reply
    0
    • D denizmercan

      I have a Dataset that has returned data from SQL Server. I would like to read in what the SQL Server type is for each Column and what the size is. For instance is the column is a nvarchar(20) I would like to be able to find out that the column has a size of 20. Is this possible? I can't seem to figure this out even using the DataColumn. DataColumn.MaxLength is returning -1. I tried also SQL Command: SELECT COL_LENGTH('MyTable','MyColumnName') AS 'MyColumnLength' it's running fine in SQL Query analyzer but it's returning 0 in C# Any idea? thanks

      T Offline
      T Offline
      tarasn
      wrote on last edited by
      #2

      You can use SQLDMO to access specific DB Schema .Add reference to SQLDMO object - can be found under COM tab - add entry named "Microsoft SQLDMO Object Library". The snippet shows how to get column details for table "Customers" in "Northwind" DB. SQLDMO.SQLServerClass sqlServer = new SQLServerClass(); //login sqlServer.Connect("MYSERVER", "username", "password"); // go through databases list foreach (SQLDMO.Database db in sqlServer.Databases) { if (! db.SystemObject) { if(db.Name=="Northwind") { // Northwind found // go through tables list foreach( SQLDMO.Table table in db.Tables ) { if(table.Name=="Customers") { // table "Customers" found // go through columns list foreach(SQLDMO.Column column in table.Columns ) { // print out column name Console.WriteLine( column.Name ); // print out column length Console.WriteLine( column.Length ); // print out column datatype Console.WriteLine( column.Datatype ); } } } } } } DevIntelligence.com - My blog for .Net Developers

      D 1 Reply Last reply
      0
      • T tarasn

        You can use SQLDMO to access specific DB Schema .Add reference to SQLDMO object - can be found under COM tab - add entry named "Microsoft SQLDMO Object Library". The snippet shows how to get column details for table "Customers" in "Northwind" DB. SQLDMO.SQLServerClass sqlServer = new SQLServerClass(); //login sqlServer.Connect("MYSERVER", "username", "password"); // go through databases list foreach (SQLDMO.Database db in sqlServer.Databases) { if (! db.SystemObject) { if(db.Name=="Northwind") { // Northwind found // go through tables list foreach( SQLDMO.Table table in db.Tables ) { if(table.Name=="Customers") { // table "Customers" found // go through columns list foreach(SQLDMO.Column column in table.Columns ) { // print out column name Console.WriteLine( column.Name ); // print out column length Console.WriteLine( column.Length ); // print out column datatype Console.WriteLine( column.Datatype ); } } } } } } DevIntelligence.com - My blog for .Net Developers

        D Offline
        D Offline
        denizmercan
        wrote on last edited by
        #3

        It's pretty good but it is working some slowly... Anyway I'm using it... thanks a lot

        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