I am trying to fetch columns, datatypes and size of columns of selected table and database from SQL Server database.
So far I am successful in fetching column names and datatypes but I want to fetch size also.
Example: Consider I have 2 columns in SQL Server database tables like below:
1) Name nvarchar(30)
3) Salary decimal(6,2)
Now I am able to fetch Name and salary with nvarchar and salary datatypes but size is not coming.
Code:
String[] columnRestrictions = new String[4];
columnRestrictions[0] = 'MyDb';
columnRestrictions[1] = 'dbo';
columnRestrictions[2] = 'Employee';
using (SqlConnection con = new SqlConnection("MyConnectionString"))
{
con.Open();
var columns = con.GetSchema("Columns", columnRestrictions).AsEnumerable()
.Select(c => new (c[3].ToString(), c.Field<string>("DATA_TYPE"))).ToList();
Result I am getting is like below:
Name nvarchar
Salary decimal
Expected result:
1) Name nvarchar(30)
3) Salary decimal(6,2)
Source code: Reference
Is it possible to get above result?