DataColumn type
-
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
-
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
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 -
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 DevelopersIt's pretty good but it is working some slowly... Anyway I'm using it... thanks a lot